Forum Moderators: coopster

Message Too Old, No Replies

drop down variables vs php

         

bagheera

11:07 pm on Feb 14, 2004 (gmt 0)

10+ Year Member



Im new to PHP but not to coding and are a stuck with a 'probably silly' problem with drop downs. How can I get the value choosen in the html drop down list to be set to a php variable later on?. It works if I use the line

echo "<input name=\"Submit\" type=\"submit\"

but then I get this submit button up, and need first to submit the drop down value to the variable before using my 'submit.gif' button I use in the php code. The only thing I want is to set the choosen drop down variable to the ($color_name) variable later to be used in the php code.

Hope you can help, Im insanelly stuck on this!
The code logic is as follows:

....
html code
....
<?php
$color1="beige";
$color2="blue";
$color3="pink";
echo "<td valign=\"top\"><form name=\"form1\"method=\"post\">";
echo"<select name='color_name' class='boxes'>";
echo"<option value=\"$color1\">$color1</option>";
echo"<option value=\"$color2\">$color2</option>";
echo"<option value=\"$color3\">$color3</option>";
echo"<input type=hidden name=hidden1 value=\"$color1\">";
echo"<input type=hidden name=hidden1 value=\"$color2\">";
echo"<input type=hidden name=hidden1 value=\"$color3\">";
echo "<input name=\"Submit\" type=\"submit\" class=\"submitbutton\" value=\"Submit\">";
echo"</select>";
echo "</form></td>";
?>
....
html code
....

php code start
....

echo "<a href=\"cart1.php action=add_item&id=$itemId&qty=1&colorid=$color_name\"><img src=\"http://www.****/submit.gif\" border=\"0\"></a>\n";

....
php code end

Knowles

5:22 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



What is your <form> tag look like? Post or Get?

If Post use $color_name = $_POST['color_name'];
if Get use $color_name = $_GET['color_name'];
Put that above the line of PHP code you pasted. This will cause it $color_name to pull the value set in the form.

btw welcome to WebmasterWorld!

zollerwagner

8:20 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



This may be obvious, although I know I've occasionally forgotten it: PHP only runs on the Web server, so if you need something to occur on the user's computer (without a return trip to the Web server) PHP won't do it.

Here's another idea for your script that another member told me about last week. This has at least one unintuitive requirement, so be sure to read up on it at php.net

.... 
html code
....
<?php
$color1="beige";
$color2="blue";
$color3="pink";
echo <<<EOD
<td valign="top"><form name="form1" method="post">
<select name='color_name' class='boxes'>
<option value="$color1">$color1</option>
<option value="$color2">$color2</option>
<option value="$color3">$color3</option>
<input type="hidden" name="hidden1" value="$color1">
<input type="hidden" name="hidden1" value="$color2">
<input type="hidden" name="hidden1" value="$color3">
<input name="Submit" type="submit" class="submitbutton" value="Submit">
</select>
</form></td>
EOD;
?>
....
html code

Everything between the first and second EOD will be echoed. You can use other expressions (I think this is basically a variable name) other than EOD. You don't have to escape your quotes or use the echo for each line. You do have to put the final "EOD" on a separate line with no whitespace in front of it. It can be used with PHP variables in it, and (I think) it can be used to create a string.

I might also add \n at the end of each line to get those lines to appear on separate lines in view source to help with debugging. I think that'd work....

By the way, you should put quotes around all of your attributes, as shown here. Some of them were missing on the hidden inputs.

bagheera

8:33 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



thank you for the replies...will try them out