Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown onchange calling PHP Function

What I'm attempting to do with the below code is call a PHP function from an drop-down 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>
like image 230
Mark James Avatar asked Feb 08 '17 08:02

Mark James


1 Answers

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 method="POST" action="">
    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($_POST["country"])){
       $country=$_POST["country"];
       echo "select country is => ".$country;
   }
?>
</body>
</html>
like image 63
vivekcs0114 Avatar answered Oct 15 '22 20:10

vivekcs0114