How to pass html option text as a value for select tag into php $_post using JQuery -
i have following select tag option tags dynamically generated php:
<select class="form-control " name="list" id="list" onchange="" > <!-- option tags dynamically generated--> </select> these sample options dynamically generated:
<option value="1">item-1</option> <option value="2">item-2</option> <option value="3">item-3</option> when use var_dump($_post) got array (size=1) 'list' => string '1' selecting option item-1.but want pass option text 'list' => string 'item-1' $_post.also need original option value.i try ajax didn't work:
$("#list").val(this.options[this.selectedindex].text);
so ajax need in correct way.thanks
you can set option value text want recieve:
<option value="item-1">item-1</option> <option value="item-2">item-2</option> <option value="item-3">item-3</option> or can option text , value this:
var optiontext = $("#list option:selected").text(); var optionvalue = $("#list").val(); and later send server via jquery.ajax() request:
$.ajax({ method: "post", url: "yourfile.php", data: { text: optiontext , val: optionvalue } }) .done(function( result ) { // handle result }); in yourfile.php you'll have $_post data:
array( 'text' => 'selected option text', 'val' => 'selected option value', )
Comments
Post a Comment