1

I have a problem with the POST method.

I have a dropdown menu with different tram stops and I have assigned a name to the dropdown menu within my form. On the next page I try to get the selected tram_stop but if I do $POST_['NameOfSelectionBox] I get nothing. Here's my code:

DROP DOWN MENU:

// list box select command
echo "<select name=$count value=''><option>Please choose desired stop. </option>";

while($r = mysqli_fetch_array($result)) {
    echo "1";
    echo '<option value='.$r['id'].'>'.$r['name_'].'</option>';
}
echo "</select>"; // Closing of list box

So name = $count, and $count is just a variable starting at 1 and going once for each new drop down menu I make, so in my understanding I should be able to get the selected value from the dropdown menu with

$_POST['1']

on my next webpage. This doesn't work and with this code

if(($_POST['1'] == 0))
{
    echo "xyz";
}

I found out that is 0 even if something has been selected and submited.

Any suggestions?

Edit: I did the recommended step to fix the problem as I declared all of my variables before using them. No improvements unfortunately.

Furthermore I think he does recognize that the section field with $_POST['1'], but it's just 0.

EDIT: SOLVED! I had to change it to '<option value='.$r['name_'].'>'.$r['name_'].'</option>';

So I don't get the id back which happend to be 0. Really dumb mistake. Thanks anyway!

d.coder
  • 1,988
  • 16
  • 23
Yíu
  • 383
  • 3
  • 14
  • What is the value of `$count`? You should give your form fields more meaningful names, such as `"tram_stop"` and not leave it up to a variable. Any change to `$count` in the rendering script will cause it to fall out of sync with the form handling code. Also, it's probably safer to enclose your select attributes in double quotes, like: `echo " – acobster Jan 08 '16 at 18:40
  • Hello and thanks for the feedback! I'm doing this with a variable because this is in a google maps like webapp for a function to add a route of 2 to n stops. So I don't know how many of these dropdown menus I will have before the user states how many he wants. Since I will have to keep working with the names I thought it's easiest to do with a count. And if that's safer I will use double quotes from now on, thanks! Edit: oh and the value of count starts with 1 and goes up by one for each dropdown menu. – Yíu Jan 08 '16 at 18:48

1 Answers1

0

Try this code

echo "<select name=$count value=\"\"><option>Please choose desired stop. </option>"; // list box select command

while($r = mysqli_fetch_array($result))
{
echo "<option value=".$r['id']." ";
if($_POST['count']==$r['name_']){ echo " selected ";}
echo ">".$r['name_']."</option>";
}
echo "</select>";// Closing of list box

have fun

  • Thanks for the suggestion! Sadly this gives me a weired error "undefined index count" that replaces all stops in the dropdown menu (so you can only select the error messages now hehe). – Yíu Jan 08 '16 at 18:52
  • Try `$_POST["$count"]` instead of `$_POST['count']` - the latter is assuming there is a field called `count`, which in my understanding is not the case... – acobster Jan 08 '16 at 19:01
  • Then I am getting an "undefined offset" error within the dropdown menu for (instead of the entries). $count=1 at that moment. – Yíu Jan 08 '16 at 19:57