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>
2
  • 2
    You can't call a PHP function with javascript. Commented Feb 8, 2017 at 8:06
  • 2
    You can call a js function on change. Then write an ajax to call the php file in js function Commented Feb 8, 2017 at 8:39

6 Answers 6

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>

Sign up to request clarification or add additional context in comments.

Comments

6

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

2 Comments

Does not seem to go to the php file to echo. Is there away it could just go there as if it was a simple link.
do you mean redirecting the page to a php file?
1

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

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

Comments

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>

1 Comment

Welcome @Prmaja Co. - you should add an explanation of what your additional/modified code does.
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>

Comments

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;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.