tags:

views:

80

answers:

4

How do I fix this so that when php calls $fontcolor and the user selects the option value = "fblue" the font color will be blue?

Likewise if the user selects the value = "size1" for Font Size 1, how do I change the font based on the user selection, since this is a radio button I think only the value will be sent to the php script, in this case size1?

The section with the php script will be on it's own separate .php file and will not be included with the html, this was just to show how the .php section of the script looks like.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Webpagegenerator
</title>
</head>
<body>
<center>

<h1>Web Page Generator</h1>
<h3>Generates a web page based on user choice:</h3>

<form method = "post"
action = "webpagegene.php">

<h3>User inputs text body: </h3>

<textarea name = "textbody"
rows = "10"
cols = "40">
</textarea>

<br/>
<br/>

<table border = "2" >
<tr>
<td><h3>Font Color</h3></td>
<td colspan = "2"><h3>Background Color</h3></td>
</tr>

<tr>
<td>
<!--How do I fix this so that when php calls  $fontcolor
and the user selects the option value = "fblue" the font color will be blue?-->
<select name = "fontcolor">
<option value = "fblue">Blue</option>
<option value = "fred">Red </option>
<option value = "fgreen">White</option>
</select>
</td>
<td>

<select size = "5"
name = "backgroundcolor">
<option value = "byellow">Yellow</option>
<option value = "bsilver">Silver</option>
<option value = "bmaroon">Maroon</option>
<option value = "bpink">Pink</option>
</select>
</td>

<!--Likewise, if the user selects the value = "size1" for Font Size 1, how 
do I change the font based on the user selection, since this is a radio button
I think only the value will be sent to the php script, in this case size1?-->
<td>
<input type = "radio"
name = "sizeType"
value = "size1"/>Font Size 1<br/>

<input type = "radio"
name = "sizeType"
value = "size2"/>Font Size 2<br/>

<input type = "radio"
name = "sizeType"
value = "size3"/>Font Size 3<br/>
</td>
</tr>
</table>

<br/>

<input type = "submit" value = "show me"/>

<br/>
<br/>
</form>

<?php
// The php script seems to work; however, in terms of 
// the script manipulating attributes such as changing 
// the font, color or background when requested by the user, it 
// does not seem to yield any results :-(.

// I can however have the user type text into the text field, but this is not
// enough, I want to be able to manipulate it-change it's font size, font color
// and the background color for the text.
$backgroundcolor = $_REQUEST["backgroundcolor"];
$fontcolor = $_REQUEST["fontcolor"];
$textbody = $_REQUEST["textbody"];
$sizeType = $_REQUEST["sizeType"];

$theStyle = <<< HERE
"$textbody$fontcolor$sizeType"
HERE;

print $textbody;
?>

</center>
</body>
</html>
A: 

You need to use a form to do the postback.

Something like:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
  <select size="5" name="backgroundcolor">
    <option value="byellow">Yellow</option>
    <option value="bsilver">Silver</option>
    <option value="bmaroon">Maroon</option>
    <option value="bpink">Pink</option>
  </select>
  <input type="submit" value="Change" />
</form>

Upon postback, you'll receive the input at $_GET['backgroundcolor'].

I would recommend the use of Javascript, Cookies and Sessions instead.

Using jQuery (Javascript Framework), and a jQuery Cookie Plugin:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
  <select size="5" name="backgroundcolor" id="bgcolor">
    <option value="yellow">Yellow</option>
    <option value="silver">Silver</option>
    <option value="maroon">Maroon</option>
    <option value="pink">Pink</option>
  </select>
  <input type="submit" value="Change" id="sbtbtn" />
</form>
<script type="text/javascript">
 <![CDATA[

  $("#sbtbtn").hide(); // hide the Change button so that
                       // you can dynamically do changes
                       // without a post back

  $("#bgcolor").change(function(){
                         var val = $("#bgcolor").val();
                         $("body").css({backgroundColor: val})
                         // changes the background color

                         $.cookie('changebgcolor', val, { expires: 10 }); 
                         // sets the cookie and value, then expire in 10 days.

                      });



 ]]>
</script>

At backend (PHP):

<?php

if(isset($_COOKIE['changebgcolor'])){
   $_SESSION['bgcolor'] = $_COOKIE['changebgcolor'];
   setcookie ('changebgcolor', '', time() - 3600);
   // unset the cookie
}

$_SESSION['bgcolor'] = $_SESSION['bgcolor']?$_SESSION['bgcolor']:'white';

echo '<style type="text/css"><!--
body{background-color:'.$_SESSION['bgcolor'].'}
--></style>';

?>
thephpdeveloper
Newb
That's a really nice and interesting use of a variable $("body") and .css, in addition to the the $.cookie, I did not know you could do that, how do you get to be that good?
Newb
you can actually embed PHP and HTML together. in fact that would be more efficient since you have will have many files if you seperate them. What's more if you need to display data in the HTML file? Self-study, internet and logic did the job.
thephpdeveloper
A: 

I think you're misunderstanding what PHP does.

The script is processed by the server before it gets sent out to a user's browser.

After the PHP script has been processed by the server, it will be in plain HTML, and you will need to employ different techniques into getting the changes you want.

Mauris suggested a few methods. Just google them and you should find lots of examples.

Some other good search terms are DOM, AJAX, and DHTML.

pxl
After the PHP script has been processed by the server, it will be in plain HTML, and you will need to employ different techniques into getting the changes you want.Yeah, techniques? On the html or php side if so, how?
Newb
I was under the assumption that the html side in this case the code I submitted would be responsible for just the options in terms of the user choosing a font size, background color and font color.The php script will sit on the server and do all the manipulations in terms of formatting the html output based on the selected choice by the user on the initial html form.
Newb
A: 

Or you can use jQuery (javascript) to change the color. If you use javascript, you also don't have to reload the page.

Here is an example http://www.smallsharptools.com/SmallSharpTools.EmbeddedScripts/JQuery.aspx

Look at the source code.

Steven
JavaScript would be nice, but I am trying to learn php, the last thing I need is some other scripting language-in other words I am trying to eat and taste my php plate before I start trying out other foods (Javascript), thanks for the idea though.
Newb
I just thought php would be easier but looking at the clock, it's not, it just seems to be a lot of hard work, but it's worth it, maybe once I start to understand php, javascript will not be that hard or so I have heard.
Newb
A: 

Hi oktet8, are You want just print tex from textarea in new screen with new style or show the same page with changed a textarea content?

Kamilos
Print text from text-area in a new screen with new style.
Newb
So You can just print something like this: print "<font color=\"".$fontcolor."\" size=\"".$sizeType."\" style=\"background-color:".$backgroundcolor.";\">".$textbody."</span>";
Kamilos