2

Before anyone flames, I have looked at many more than just the following 3 posts. None of these have working answers.

How to Post data from a php database populated Drop Down to another script?

Passing value from a dropdown list from one php to another

What is the best way to have a dynamic dropdownlist in PHP and POST selected value

These simply just do. not. work.

Now on to my question:

I have a dropdown list on my php page that is being populated by a MySQL query. This part works perfectly. However -- I can not get the selected value from the dropdown list to post in to my next PHP script.

My code on "page1.php":

    Please select your dietary preference: 


    <form method="post" action="page2.php">
        <tr>
            <td>
                <select name="dropdown_value">
                    <?php foreach ($data as $row): ?>
                        <option><?=$row["dietary_option"]?></option>
                    <?php endforeach ?>
                </select>
            </td>
        </tr>
    </form>

This successfully populates my dropdown list with the available dietary options pulled from my database.

And on page2.php:

    $mealSelection = $_POST["dropdown_value"];

Simply does nothing. It yields no value(?). Echoing $mealSelection seemingly echoes nothing. It is blank.

I would ultimately like to use $mealSelection in a new query on page2.php but I can't seem to retrieve the selected value from page1.

Any wizards with some insight?

Thanks for taking the time to look at my issue here.

LC-Data

Edit: None of the answers for simply posting dropdown options from predefined HTML work. I specifically need the dropdown to be populated from my db.

LC-Data
  • 80
  • 9
  • Possible duplicate of [Using $\_POST to get select option value from HTML](https://stackoverflow.com/questions/17139501/using-post-to-get-select-option-value-from-html) – Carl Binalla Feb 23 '18 at 05:39
  • Place value attribute to option tags. Than let us know ? – Adnan Haider Feb 23 '18 at 05:53
  • Thanks Adnan, good eye. Amit below was able to correctly identify that as well. This was indeed what was holding me back. – LC-Data Feb 23 '18 at 06:03

1 Answers1

3

Use it like this:

<?php foreach ($data as $row): ?>
         <option value="<?= $row['dietary_option'] ?>"><?=$row["dietary_option"]?></option>
<?php endforeach ?>

You haven't given value field in your options. Without value field, it won't work.

Amit Merchant
  • 1,045
  • 6
  • 21
  • Hey Amit, I will be momentarily selecting yours as the correct answer. What a horrible oversight! Thanks for taking the time to debug that. – LC-Data Feb 23 '18 at 06:01