Björn C
Björn C

Reputation: 4008

Fetch value and name in a select, from form

I'm trying to fetch several data from a select list. I have the value as $_POST['valt_skift']. But I also like to send name. How can I do this?

This is what I got:

<select id="valt_skift" name="valt_skift">
  <option></option>
  <?php
    //Loop put result
    while($row = $stmt->fetch()){
      echo '<option name="skift_name['.$row['skift_name'].']" value="'.$row["skift_type"].'">'.$row["skift_namn"].'</option>';
    }
  ?>
</select>

The fetch:

$query_params = array( 
    ':usr_skift' => $_POST['skift_name'][0], //Here is my question?!
    ':usr_skift_type' => $_POST['valt_skift']); //this will be the value

Upvotes: 1

Views: 48

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You have typo within your code, you can't have name attribute within option value.If you want to get two values from the selected option then you can send it as

echo "<option value='{$row['skift_type']}@@@{$row['skift_name']}'>{$row['skift_name']}</option>";

This way you'll get the values along with the name within your $_POST['valt_skift'] as

echo $_POST['valt_skift']; // your_value@@@your_name

now you can get two values within one select option using explode you can fetch them both as

$get_skift = explode('@@@',$_POST['valt_skift']);
$usr_skift_type = $get_skift[0];//your_value
$usr_skift = $get_skift[1];//your_name

Upvotes: 1

Related Questions