Forum Moderators: coopster
I have a profile page where members can select a photo that has been already uploaded. These photos are given using a pull down menu. The problem is that when a photo is selected I can't get the selection into a variable even though I'm using $_POST[], in what seems to be the appropriate location. Would appreciate any insight into what I'm doing wrong.
The listed code is a simplified version of what I'm attempting. Thanks!
<?php
$photo_list=array('Me_summit_Cotopaxi.jpg', 'Me_Compressed.jpg', 'Picture 4.jpg');
if (isset($_POST['submit_btn']))
{
$select=$_POST['photo_select'];
echo 'New Selection: '.$select;
}
else
{
$new_select=1;
}
echo "<form name=\"form1\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
$drop='<p><select name=\"photo_select\" size=\"1\">';
for ($i=0; $i<=2; $i++)
{
if ($select==$i)
{
$drop .= '<option selected value=".$i.">'.$photo_list[$i].'</option>';
}
else
{
$drop .= '<option value=".$i.">'.$photo_list[$i].'</option>';
}
}
$drop .= '</select>';
echo $drop;
echo "</p><p align=\"center\"><input type=\"submit\" name=\"submit_btn\" value=\"Submit\" style=\"font-weight:bold;
text-decoration:underline;\"></p></form>";
?>
You need to add some single quotes to concatenate the value of $i properly:
$drop .= '<option selected value="'.$i.'">'.$photo_list[$i].'</option>';
}
else
{
$drop .= '<option value="'.$i.'">'.$photo_list[$i].'</option>';
If I choose the third choice in the array, then the submit button, I'll see the first item in the array in the dropdown list. And of course the echo for the New Selection is empty, which explains why I see that first item in the array.
I really don't understand why my selection isn't getting posted to my variable.
Btw, the $new_select=1 has been changed to $select=1.
Ice
remove the backslashes.
When you're working with strings and you encapsulate them with single quotes, the contents aren't evaluated, that is, a backslash is just a backslash, not part of an escape sequence. Surround them with double-quotes if you need to escape characters or want php to evaluate variables inside - for example, the $drop concatenation in the else could also be written this way (which is the style you used to open the form):
$drop .= "<option value=\"$i\">{$photo_list[$i]}</option>";