AJAX PHP function onchange select box

前端 未结 5 1801

I have a problem about which I am very confused. I have a select box with s dynamically generated using a mysqli query:

$result = mysqli_query($db, \"SELECT         


        
相关标签:
5条回答
  • 2021-01-16 23:18

    Your select box has no ID and you are watching the change event of $("#contacts").

    Change:

    echo '<html><form name="contacts" method="post"><select name="contacts"><option value="Contact list" onchange="func()">Contact List</option>';
    

    to:

    echo '<html><form name="contacts" method="post"><select name="contacts" id="contacts"><option value="Contact list">Contact List</option>';
                                                                            ^^^^^^^^^^^^^ here
    

    You also only need one event handler, so I have removed the inline one which doesn't seem to do anything anyway.

    Edit: If the select is created using ajax as well, you need event delegation:

    $("body").on('change', '#contacts', function() {
       ^^^^ for example
    

    Edit 2: Your variable is called $_REQUEST['option'] and not $_REQUEST['selectedValue']. You are also not calling your -badly named - function so you will not get any output from php except from an error like Parse error: syntax error, unexpected 'function' ....

    0 讨论(0)
  • 2021-01-16 23:20

    Call onchange function in select tag as below

    echo '<form name="contacts" method="post"><select name="contacts" onchange="func(this.value)"><option value="Contact list">Contact List</option></form>';
    

    Javascript src should be in head of the html page.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    

    Add the above one in head of html. Update javacript as below

    As onchange function is called in the select tag itself, following is enough

    <script>
    
    function func(selectedValue)
     {
        //make the ajax call
        $.ajax({
            url: 'function.php',
            type: 'POST',
            data: {option : selectedValue},
            success: function() {
                console.log("Data sent!");
            }
        });
    }
    </script>
    

    Updated php: If you must want to get value from function, you must call it. Otherwise, simply, you can make as below

        <?php
        if($_REQUEST['option'])
        {
        $val=$_REQUEST['option'];
    echo $val;
         }
        ?>
    
    0 讨论(0)
  • 2021-01-16 23:21

    set an id attribute in your php code for the select tag and

    please don't use the same value for the name attribute in form and select tags !!

    0 讨论(0)
  • 2021-01-16 23:34

    In .php file, receive it first-

    $val = $_REQUEST['selectedValue'];
    echo $val;
    
    0 讨论(0)
  • 2021-01-16 23:34

    just change your function to a 'body'.on, and give your elements an id of 'contacts'

    $("body").on('change', '#contacts', function() {
        //get the selected value
        var selectedValue = $(this).val();
    
        //make the ajax call
        $.ajax({
            url: 'function.php',
            type: 'POST',
            data: {option : selectedValue},
            success: function() {
                console.log("Data sent!");
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题