php vs html drop down problem
Moderator: General Moderators
php vs html drop down problem
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
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
Is the line:
On the same page as that form?
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";
?>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 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...
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...
- Michael 01
- Forum Commoner
- Posts: 87
- Joined: Wed Feb 04, 2004 12:26 am
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:
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:
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.
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!");
?>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!");
?>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:
add this at the beginning if you want to test the script:
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>";
?>Code: Select all
<?php
$color_name=$_POST["color_name"];
if (isset($_POST["Submit"])) {
echo $color_name;
}
?>