Sudeep Sudeep - 3 months ago 10
HTML Question

How to post dynamic dropdown value in php

I have using the following code for my dropdown

<select class="form-control" name="device" id="device" onChange="getBrand(this.value);">
<option selected name="device" id="device" value="<?php echo $row['id'] ?>"><?php echo $row['name']; ?></option>
</select>


for form submit, i have used this code

if(isset($_POST['submit'])){
extract($_POST);
$device = $_POST['device'];
}


I want to get selected value in button submit, currently I got the id instead of value.

Thanks in advance.

Answer

You have to put $row['name'] as value of your option

<select class="form-control" name="device" id="device" onChange="getBrand(this.value);">
    <option selected name="device" id="device" value="<?php echo $row['name'] ?>"><?php echo $row['name']; ?></option>
</select>

If you can't change option value, you could use jQuery:

$("#your_form").submit( function () {
    var option_text = $('#device').find(":selected").text();
    $("#your_form").append('<input name="selected_option_text" type="hidden" value="' + option_text + '">');
});

This will add a input field with name "selected_option_text" before submit. So you will find it in your receiving script

Comments