10

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>
xxx
  • 1,153
  • 1
  • 11
  • 23
Mark James
  • 481
  • 2
  • 6
  • 18

6 Answers6

17

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>
Vivek Tiwari
  • 437
  • 4
  • 9
5

simple ajax using jquery

index page

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
            $('#myDropDown').change(function(){
                //Selected value
                var inputValue = $(this).val();
                alert("value in js "+inputValue);

                //Ajax for calling php function
                $.post('submit.php', { dropdownValue: inputValue }, function(data){
                    alert('ajax completed. Response:  '+data);
                    //do after submission operation in DOM
                });
            });
        });
        </script>
    </head>
<body>
    <select id="myDropDown">
        <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>

</body>
</html>

in submit.php

<?php
function processDrpdown($selectedVal) {
    echo "Selected value in php ".$selectedVal;
}        

if ($_POST['dropdownValue']){
    //call the function or execute the code
    processDrpdown($_POST['dropdownValue']);
}

for simple js ajax use XMLHttpRequest

Santhy K
  • 829
  • 1
  • 7
  • 12
1

Does not connect onchange event with php function. must use javascript function

<script>
 function OnSelectionChange()
 {
  alert("OK IT WORKS");
 }
</script>
1

<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>
Prmaja Co.
  • 11
  • 1
  • 1
    Welcome @Prmaja Co. - you should add an explanation of what your additional/modified code does. – KJH Sep 19 '18 at 14:02
0

Can't do that, use JavaScript function instead

<html>
<head>
</head>
<body>

<script>
function OnSelectionChange()
{
 alert("OK IT WORKS");
}
</script>

<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>
0

This mild adaptation has worked well for me. So very many thanks to vivekcs0114 and furthermore nicely avoids JS and the required Ajax solution which after around 200 miserable attempts has been a total disaster.

<form method="post" action="">
<select method="post" name="areasel" onchange="this.form.submit()"> 
<option value="Choose one">Choose one</option>
<option value="Complete Airframe">Complete Airframe</option>
<option value="Armstrong Siddeley">Armstrong Siddeley</option>
<option value="Something">Something</option>
</select>
</form>
<?php
if(isset($_POST["areasel"]))
{
$type=$_POST["areasel"];
echo "<BR>Do some SQL stuff using :".$type;
}
?>
RayH
  • 1
  • 3