Anand Sunderraman Anand Sunderraman - 6 months ago 4x
PHP Question

HTML Select selected value in php variable

I have a drop down in html.

When I change the value of the drop down I want to store the selected value in a php variable

How can i achieve this ?


Technically it is impossible. PHP is server side, HTML is client side. When a user calls up a URL the server runs the PHP which gets sent to the HTML, so you can not go backwards.

You can however use ajax to get the value of the select box and use it in some meaningful way. What are you wanting to do with this variable?


Using jQuery I would send the value of the select box to an ajax file, and then have the ajax file create the text box if the value is what you want it to be. Here is an example.

$("#selectEle").change(function() {
        type: "POST",
        url: "yourajaxpage.ajax.php",
        data: {selectVal: $(this).val()},
        success: function(response) {
        error: function() { alert("Ajax request failed."); }

You will grab the value in your ajax page using $_POST['selectVal']. Test that value there, and create an HTML textbox which gets sent back to the success function.