-2
<form>
    Main 
       <select name="main">
               <option value="a">A</option>
               <option value="b">B</option>
               <option value="c">C</option>
               <option value="d">D</option>
       </select>
       <br/>
       <input type="submit" name="submit" value="Order">
</form>

How do I output the user's selected option using var_dump in php? And does $_POST work?

dave
  • 11,641
  • 5
  • 47
  • 65

2 Answers2

1

Use the Below Code.You have made some mistake like the option values are set wrongly.Inside the form tag it is advisable to use the action and method attribute.

<?php
    if(isset($_POST['submit'])){
        $mainValue = $_POST['main'];//Retrieve the Option Value;
        echo var_dump($mainValue);//Use var_dump
    }
?>

<form method="post">
    Main 
    <select name="main">
        <option value="A">a</option>
        <option value="B">b</option>
        <option value="C"></option>
        <option value="D">d</option>
    </select><br/>
    <input type="submit" name="submit" value="Order">
</form>
0

form action attribute tag means where the form shall be submitted when submit button is press

form method attribute tag means type of server request method being requested

<form action="" method= "POST">
Main <select name="main">
  A <option value = "A">a</option>
  B <option value=="B">b</option>
  C <option value= ="C">c</option>
  D <option value ="D">d</option>
</select>

<br/>

<input type="submit" name="submit" value="Order">
</form>


<?php 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        // this will print information about the content of the form
        // being submitted
        var_dump($_POST);
        // or
        echo '<br/>';
        print_r($_POST);

    }

?>
Oli Soproni B.
  • 2,774
  • 3
  • 22
  • 47