0

I need help.

I have a select box with items and I want, if the user select a car name and click on "search" I need to have the name of the car he selected on a "echo".

I tried couple of things but it displays nothing (blank page). below is my code.

Thanks in advance !

<select name="category" >
    <option value="nocategory">No Category</option>
    <option value="toyota">Toyota</option>
    <option value="nissan">Nissan</option>
    <option value="mazda">Mazda</option>
    <option value="volvo">Volvo</option>
 </select>

<input name="submit"  value="Search" type="submit">

</form> 


<?php

   if (isset($_POST['submit'])){
   $category = $_POST['category'];

?>

3
  • Possible duplicate of how to get values from HTML select to php\sql Commented Mar 25, 2017 at 11:16
  • SHow us your <form> tag Commented Mar 25, 2017 at 11:21
  • 1
    blank page, because you are not closing your if statement brackets; Commented Mar 25, 2017 at 11:27

1 Answer 1

0

Set form action to $_SERVER['PHP_SELF'] like below: $_SERVER['PHP_SELF'] is a PHP super global variable which returns the filename of the currently executing script.

   <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="category" >
        <option value="nocategory">No Category</option>
        <option value="toyota">Toyota</option>
        <option value="nissan">Nissan</option>
        <option value="mazda">Mazda</option>
        <option value="volvo">Volvo</option>
     </select>

    <input name="submit"  value="Search" type="submit">

   </form> 


    <?php

       if (isset($_POST['submit'])){
       $category = $_POST['category'];
       echo $category;
       }

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

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.