Dropdown onchange calling PHP Function

Mark James picture Mark James · Feb 8, 2017 · Viewed 70.9k times · Source

What i'm attempting to do with the below code is call a PHP function from an dropdown menu.

Is there a clean way of doing this?

code:

<html>
<head>
</head>
<body>
   <?php
     function OnSelectionChange() {
       echo("OK IT WORKS");
     }    
  ?>
  <section>
    <select onchange="OnSelectionChange()">
      <option value='' disabled selected>Assign Driver</option>
      <option value='4353'>Steve Jobs</option>
      <option value='3333'>Ian Wright</option>
      <option value='66666'>Mark James</option>
    </select>
  </section>    
</body>
</html>

Answer

vivekcs0114 picture vivekcs0114 · Aug 16, 2017

You can get the selected value from the drop down list simply using php without using JavaScript.

<html>
<head>
<title>Country</title>
</head>
<body>
<form>
    Select Your Country 
    <select name="country" onchange="this.form.submit()">
        <option value="" disabled selected>--select--</option>
        <option value="india">India</option>
        <option value="us">Us</option>
        <option value="europe">Europe</option>
    </select>
</form>
<?php
   if(isset($_GET["country"])){
       $country=$_GET["country"];
       echo "select country is => ".$country;
   }
?>
</body>
</html>