简体   繁体   中英

ajax function on change dropdown Value

Hie Everyone! In PHP page1 my code is here..

    <html>
    .
    ...
    <select id="customer">...</select>
    ..
    ....
    <div id="show"></div>
    //and Java script function (ajax call)
   <script>
   $('#customer').change(function(){
        var Id = $(this).val();
    $.ajax({
        type: "GET",
        url: "page2.php",
        data: "ID="+id,
        success: function( data ) {
            document.getElementById("show").innerHTML = data;
        }
    });
    });
    </script>
    </html>

In php page2 as code..

<?php
$ID=$_GET['ID'];
...
//db connection code
..
$sql="select * from Table1 where id='$ID'";
//result code..
//while loop..
//echo something..
// all working without error..
?>

So, when I was trying to do this.It does not showing the success data or may be Ajax function not work.I had check with alert(data); but does not Alert anything. please help.

You will give echo infront of the $get_id variable. But you will make sure only one echo in the page2.php page.

<?php
echo $get_id=$_GET['pass_id'];
...
//db connection code
..
$sql="select * from Table1 where id='$get_id'";
//result code..
//while loop..
//echo something..
// all working without error..
?>

Then in page1.php check your ajax response. using alert function.

 <script>
   $('#customer').change(function(){
        var id = $(this).val();
    $.ajax({
        type: "GET",
        url: "page2.php",
        data: "pass_id="+id,
        success: function( data ) {
              alert(data);

            document.getElementById("show").innerHTML = data;
        }
    });
    });
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM