Forum Moderators: coopster

Message Too Old, No Replies

Pull Down & Showing Selection

         

ice_screw

9:16 pm on May 9, 2008 (gmt 0)

10+ Year Member



I've been stymied by my problem for the last couple of days and everything I've read tells me it should work.

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

cameraman

10:05 pm on May 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at the resulting HTML in your browser? The problem is here:
$drop .= '<option selected value=".$i.">'.$photo_list[$i].'</option>';
}
else
{
$drop .= '<option value=".$i.">'.$photo_list[$i].'</option>';

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>';

ice_screw

1:36 am on May 10, 2008 (gmt 0)

10+ Year Member



Cameraman,
Adding the single quote marks as you suggested didn't change the output in my browser.

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

cameraman

6:49 am on May 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here:
$drop='<p><select name=\"photo_select\" size=\"1\">';

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

ice_screw

9:53 am on May 10, 2008 (gmt 0)

10+ Year Member



Thanks so much! I took out the backslashes in the statement as you suggested and changed
$drop .= '<option value=".$i.">'.$photo_list[$i].'</option>';
to
$drop .= '<option value='.$i.'>'.$photo_list[$i].'</option>';
Obviously I need to do some homework on single and double quote marks in strings.
Thanks again for clearly up my headache.