0

i want to echo selected parent value. but i am getting error- Notice: Undefined index: How can i echo selected parent value then? Whats wrong i am doing?

$q = mysql_query("SELECT * FROM menu");
    echo '<form action="" method="post">
      Menu name:<input type="text" name="mname"><br>
      <select>';
    while ($row = mysql_fetch_array($q)) {
        $menu_name = $row['menu_name'];

    echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
    }
    echo '</select><br>
      <input type="submit" name="submit" value="Add Menu">
    </form>';



    if (isset($_POST['submit'])) {
        echo $mname = $_POST['mname'];
        echo $parent = $_POST[$menu_name];
    }
4
  • 1
    add name attribute to select tag. Commented Jun 9, 2016 at 14:34
  • Note: mysql_query() and mysql_fetch_array() are deprecated, please use mysqli_query() and mysqli_fetch_array() in the future Commented Jun 9, 2016 at 14:34
  • $_POST[$menu_name]? $menu_name is only ever going to contain the LAST item you fetched from the db. And since your <select> has no name, it'll never get submitted with the rest of the form anyways. Commented Jun 9, 2016 at 14:40
  • WARNING: If you're just learning PHP, please, do not use the mysql_query interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way explains best practices. Commented Jun 9, 2016 at 15:28

3 Answers 3

1

add name to the select box and get the value of select box by name. Updated code:-

$q = mysql_query("SELECT * FROM menu");
echo '<form action="" method="post">
  Menu name:<input type="text" name="mname"><br>
  <select name="menu_name">';
while ($row = mysql_fetch_array($q)) {
    $menu_name = $row['menu_name'];

echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
  <input type="submit" name="submit" value="Add Menu">
</form>';



if (isset($_POST['submit'])) {
    echo $mname = $_POST['mname'];
    echo $parent = $_POST['menu_name'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

$_POST[$menu_name] probably doesn't exist, because only two elements in your form have name attributes. The text input and the submit input.

option elements aren't posted as part of the form, but rather the selected option's value for the select element. But your select element has no name, therefore no key to use in the key/value pair, so it isn't posted.

Give the element a name:

<select name="someName">

Then in the POST, you would be able to fetch the selected value just as you do for any other form element:

$_POST['someName']

Comments

0

You need to add name attribute to select tag.

echo '<form action="" method="post">
  Menu name:<input type="text" name="mname"><br>
  <select name="any_name">';
$q = mysql_query("SELECT * FROM menu");
while ($row = mysql_fetch_array($q)) {
    $menu_name = $row['menu_name'];
    echo '<option value="'.$menu_name.'">'.$menu_name.'</option>';
}
echo '</select><br>
  <input type="submit" name="submit" value="Add Menu">
</form>';



if (isset($_POST['submit'])) {
    echo $mname = $_POST['mname'];
    echo $select_option_name = $_POST['any_name'];
}

Note: mysql_* functions are depricated, use mysqli_* functions

Comments

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.