php vs html drop down problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

php vs html drop down problem

Post by serap »

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.xxx/submit.gif\" border=\"0\"></a>\n";

....
php code end
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Is the line:

Code: Select all

<?php
echo "<a href="cart1.php action=add_item&id=$itemId&qty=1&colorid=$color_name"><img src="http://www.xxx/submit.gif" border="0"></a>\n"; 
?>
On the same page as that form?
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

Post by serap »

yes it is - so it SHOULD be simple!? Im just blind at the moment...
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Code: Select all

<?php
if($_POST['Submit'] == "Submit")  //this means the form has been submitted
{
echo "<a href="cart1.php action=add_item&id=$itemId&qty=1&colorid=" . $_POST['color_name'] . ""><img src="http://www.xxx/submit.gif" border="0"></a>\n";
}
?>
I think that'll work.
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

Post by serap »

I think I dit not quite understand,

If I ommit the following:

echo "<input name=\"Submit\" type=\"submit\" class=\"submitbutton\" value=\"Submit\">";

how can I post the drop down variables ($color(n)) into the $color_name variable?

It seems I do not manage to pass the variables corerectly still...thanks for your replies...
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

I read this post over at least three times, and I am still confused.


For testing purposes, echo all of your variables to see if they are being assigned or not. Something like this actually:

Code: Select all

<?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>"; 

print("$color1 || $color2 || $color3   Varibles are being passed!");
?>
Dont hit the submit button or anything like that for this first test. Just see if they are being passed from their first assignment, than you will know if you have to use the other method of variables such as Duff pointed out.

than, if that works, try this:

Code: Select all

<?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>"; 

print("$hidden1 The hidden Varible has been passed!");
?>
Why? Simply because the real variable is "hidden1" and should be the variable that needs to be really picked up, and not the $color_name. But, that is a rough overview of your code, and like I said before, this is a confusing bit of code you have illustrated with really no other code to go with it for a real thorough overview or proper diagnosis of why the variables are not being passed.
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

I'm a bit confused with this code.
Why is the </select> not after the <option> tags ?
Why is the hidden varible always hidden1? It keeps getting reset.
Did you check if global vars are set ON ? Try your form with GET first if you want to see what is passed on.
I made a few changes:

Code: Select all

<?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"</select>"; 
echo"<input type=hidden name=color1 value="$color1">"; 
echo"<input type=hidden name=color2 value="$color2">"; 
echo"<input type=hidden name=color3 value="$color3">"; 
echo "<input name="Submit" type="submit" class="submitbutton" value="Submit">"; 
echo "</form></td>"; 
?>
add this at the beginning if you want to test the script:

Code: Select all

<?php
$color_name=$_POST["color_name"];
if (isset($_POST["Submit"])) {
 echo $color_name;   
}

?>
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

Post by serap »

ok, thanks for the replies Ill try them out
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

Post by serap »

what I did was to remove the php code and it works by using the

<td valign="top"><form name="form"method="post"action="cartser.php">

I can echo the color_name variable there, but how do I send another constant variable to cartser.php ? from the html code?

thanks
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Send it as a hidden value:
<input type=hidden name=color1 value="$color1">
Post Reply