简体   繁体   中英

Call a PHP script when an option is selected from a select element in HTML

At the moment I have the following markup code on my website:

<select>
   <option>1</option>
   <option>2</option>
   <option>3</option>
</select>

I want to have a PHP script in the same directory as my webpage fire when I select a particular option. How would I go about doing this? Thank you.

call a ajax on change of select like this

<select onchange="javascript:callAjax(this.value);">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
</select>

<script>
function callAjax(sel_opt)
{
   var url = "your_php_file.php?select_option="+sel_opt;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            var result = xmlhttp.responseText;
            // put your result where ever you want
        }
    }

    xmlhttp.open("GET",url,true);

    xmlhttp.send();
}
</script>

your_php_file.php

<?php

// your code

?>

Do an Ajax call. Here 's an example. hope this helps to understand.

cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM