0

I am creating a drop-down menu in which the second drop-down is dependent upon the first; the first drop down is a list of table, and depending on what table you select, the columns of that table will be able to be selected.

<form id = "table_column" action = "file_name.php">
    <select id = "tables" name = "tables" onclick = "script();">
        <option name = "table_option_one" value = "people">people</option>
        <option>...</option>
    </select>
    <select id = "columns" name = "columns" onclick = "other_script();">
        <option name = "column_option_one" value = "name">name</option>
        <option>...</option>
    </select>
    <input type = "submit"></input>
</form>

I have tried the code below to no success.

$table_name = $_POST["tables"];
$column_name = $_POST["columns"];

How do I access the values selected in each drop-down menu in PHP?

I am only having a problem with the retrieval of the selected values. The menus depending on another and switching work fine

3
  • You have to do this in Javascript. PHP runs on the server, not the client. Commented Jul 2, 2014 at 19:17
  • Please add your code. Commented Jul 2, 2014 at 19:17
  • @timtour97 your form doesn't have the method="post", if you don't add this attribute to the form, the values will be accessible with the $_GET variable Commented Jul 2, 2014 at 19:34

3 Answers 3

0

Okay, with your new clarification (you are populating the drop-downs fine), here is what you can do.

Give the input tag an ID as such:

<input type="submit" name="submit" id="submit" value="Submit"> <!--form submit button-->

and in your PHP do a conditional as such:

if (isset($_POST['submit'])) {
    $table_name = $_POST["tables"];
    $column_name = $_POST["columns"];
}

make sure your form also has the method="post" attribute as well:

<form id = "table_column" action="file_name.php" method="post">

That way when the submit button is hit, your values will be retrieved.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding the method attribute to the form did the trick! Thanks!
0
  1. Use AJAX to get the data for the second menu by passing the value of the first menu to a PHP script.
  2. Use the AJAX success function to populate the second menu.
  3. On submission of the form, use PHP to get the values as normal, i.e. $_POST['tables'] and $_POST['columns']

2 Comments

I have already down the first two steps, what I am looking for is the fact that I have a problem with your third step. I have tried accessing the selected values with $_POST['tables'] and I am getting this error: Undefined index: tables on line 6.
add method="post" to your <form> element.
0

This is more a job for Javascript than PHP. The simplest solution (not involving AJAX) would be to code all possible second dropdown lists, make them invisible (using CSS), then make the appropriate one appear upon a change in the first dropdown.

1 Comment

I am able to switch the drop-down menus using javascript. I am having a problem getting the values selected in each drop-down on the PHP side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.