Skip to content
Advertisement

get value from select inside $post request

Could someone help my with an issue I have? I have made an select list with different options, based on the option the user did choice I want to run a certain function so i can later show the data which i have already saved in my database.

However how do I do this? right now I have been working with: if(!empty($_POST["option1"])) to check which option is selected but the code will never get inside those if statements. the code stops after: if(!empty($_POST["confirmOption"]))

<form method="post" action="index.php"> 
            <select id="list" name="list">
              <option selected><--select option--></option>
              <option value="option1">test1</option>
              <option value="option2">test2</option>
              <option value="option3">test3</option>
              <option value="option4">test4</option>
            </select>

            <button name="confirmOption">confirm option</button
          </form>

if($_SERVER["REQUEST_METHOD"] == "POST"){
if(!empty($_POST["confirmOption"])){
      if(!empty($_POST["option1"])){
        $conn = openDatabase();
        $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option1'");
        $query->execute();
        return $query->fetchAll();
      }elseif(!empty($_POST["option2"])){
        $conn = openDatabase();
        $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option2'");
        $query->execute();
        return $query->fetchAll();
      }elseif(!empty($_POST["option3"])){
        $conn = openDatabase();
        $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option3'");
        $query->execute();
        return $query->fetchAll();
      }elseif(!empty($_POST["option4"])){
        $conn = openDatabase();
        $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option4'");
        $query->execute();
        return $query->fetchAll();
      }
    }
  }

Advertisement

Answer

You need to access it via $_POST[‘list’]. For instance, if I were using your form and I chose the second list item (not the first one without a value), then the value of $_POST[‘list’] would be set to ‘option1’.

You’ll need to modify your if condition. It should look something like this:

if ( !empty($_POST["confirmOption"]) && isset($_POST['list'] ){

    $selectedItem = $_POST['list'];
    $conn = openDatabase();

    if ($selectedItem == 'option1'){
      $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option1'");
    } elseif ($selectedItem = 'option2'){)){
      $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option2'");
    } elseif ($selectedItem == 'option3'){
      $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option3'");
    } elseif ($selectedItem == 'option4'){
      $query = $conn->prepare("SELECT * FROM choices WHERE status = 'option4'");
    } else {
        return false
    }

    $query->execute();
    return $query->fetchAll();
}

I removed some of the duplication, because if an item is selected we know that we’ll have to open the database, and ultimately will need to execute and return the query. You could even probably remove the duplicated lines where you’re setting the query statement multiple times.

To clean it up even further – if the only thing that will be changing is the select value and you know the input is safe you could reliably do something like $conn->prepare("SELECT * FROM choices WHERE status = {$selectedItem}");

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement