Dropdown onchange calling PHP Function

倖福魔咒の 提交于 2021-02-07 02:48:33

问题


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>

回答1:


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:


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>



回答3:


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

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



回答4:


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>



回答5:


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


来源:https://stackoverflow.com/questions/42107672/dropdown-onchange-calling-php-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!