簡體   English   中英

下拉AJAX,PHP和MYSQL

[英]Drop down AJAX, PHP and MYSQL

我使用了來自w3school http://www.w3schools.com/Php/php_ajax_database.asp的代碼。 我嘗試過,它有3種形式。 但是第二個不起作用。 如果我在第二個下拉菜單中選擇類別,則結果將為NULL。 我不知道應該替換什么代碼。 我是AJAX代碼的初學者。 有人可以幫助我嗎?

主要形式

     <?php
     include('connect.php');
     ?>

     <html>
     <head>

     <script>
     function showUser(str)
     {
     if (str=="")
       {
       document.getElementById("txtHint").innerHTML="";
       return;
       } 
     if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       }
     else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     xmlhttp.onreadystatechange=function()
       {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
         document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
       }
     xmlhttp.open("GET","getuser.php?q="+str,true);
     xmlhttp.send();
     }
     </script>
     </head>
     <body>

     <form>
     <select name="users" onchange="showUser(this.value)">
     <option value=''>Select a person:</option>
     <?php
     $res = mysql_query("SELECT * FROM cars group by car_year order by car_year desc");
     while($result = mysql_fetch_assoc($res)){
     $car_year = $result["car_year"];
     echo" <option value='$car_year'>$car_year</option>";
     }
     ?>
     </select>
     </form>
     <br>
     <div id="txtHint"><b>Person info will be listed here.</b></div>

     </body>
     </html>

getuser.php

     <html>
     <head>

     <script>
     function showUser(str)
     {
     if (str=="")
       {
       document.getElementById("txtHint").innerHTML="";
       return;
       } 
     if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       }
     else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     xmlhttp.onreadystatechange=function()
       {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
         document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
       }
     xmlhttp.open("GET","getuser2.php?m="+str,true);
     xmlhttp.send();
     }
     </script>
     </head>
     <body>
     <select name='users2' onchange='showUser(this.value)'>
     <option value=''></option>
     <?php
     $q = strval($_GET['q']);

     $con = mysqli_connect('localhost','root','','matterhorn');
     if (!$con)
       {
       die('Could not connect: ' . mysqli_error($con));
       }

     mysqli_select_db($con,"ajax_demo");
     $sql="SELECT * FROM cars WHERE car_year = '".$q."'";

     $result = mysqli_query($con,$sql);

     while($row = mysqli_fetch_array($result))
       {
       $car_brand = $row['car_brand'];

       echo"<option value='$car_brand'>$car_brand</option>";

       }


     mysqli_close($con);
     ?>
     </select>
     <br><br>
     <div id="txtHint"><b>Person info will be listed here.</b></div>
     </body>
     </html>

getuser2.php

     <?php
     $m = strval($_GET['m']);

     $con = mysqli_connect('localhost','root','','matterhorn');
     if (!$con)
       {
       die('Could not connect: ' . mysqli_error($con));
       }

     mysqli_select_db($con,"ajax_demo");
     $sql="SELECT * FROM cars WHERE car_brand = '".$m."'";

     $result = mysqli_query($con,$sql);


     echo"<select name='users3' onchange='showUser(this.value)'>";
      echo"<option value=''></option>";


     while($row = mysqli_fetch_array($result))
       {
       $car_brand = $row['car_brand'];

       echo"<option value='$car_brand'>$car_brand</option>";

       }
     echo "</select>";

     mysqli_close($con);
     ?>

您已將javascript function in ajax file ..這將不起作用...,並且您創建了two javascript functions of same name ...這也是個問題...請嘗試....

connect.php

<?php
$con = mysqli_connect('localhost', 'root', '', 'matterhorn');
if (!$con)
{
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con, "ajax_demo");
?>

index.php

 <?php
 include('connect.php');
 ?>

 <html>
 <head>

    <script>
        function showUser(str)
        {
            if (str == "")
            {
                document.getElementById("txtHint").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "getuser.php?q=" + str, true);
            xmlhttp.send();
        }

        function showUser2(str)
        {
            if (str == "")
            {
                document.getElementById("txtHint2").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "getuser2.php?m=" + str, true);
            xmlhttp.send();
        }

    </script>
</head>
<body>

    <form>
        <select name="users" onchange="showUser(this.value)">
            <option value=''>Select a person:</option>
            <?php
            $res = mysql_query("SELECT * FROM cars group by car_year order by car_year desc");
            while ($result = mysql_fetch_assoc($res))
            {
                $car_year = $result["car_year"];
                echo" <option value='$car_year'>$car_year</option>";
            }
            ?>
        </select>
    </form>
    <br>
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    <div id="txtHint2"></div>

</body>
</html>

獲取user.php

<?php include(connect.php); ?>

 <select name='users2' onchange='showUser2(this.value)'>
<option value=''></option>
<?php
$q = strval($_GET['q']);

$sql = "SELECT * FROM cars WHERE car_year = '" . $q . "'";

$result = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($result))
{
    $car_brand = $row['car_brand'];

    echo"<option value='$car_brand'>$car_brand</option>";
}
?>
</select>
<br><br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

和getuser2.php

<?php
include('connect.php');
$m = strval($_GET['m']);
$sql = "SELECT * FROM cars WHERE car_brand = '" . $m . "'";

$result = mysqli_query($con, $sql);

echo"<select name='users3'>";
echo"<option value=''></option>";

while ($row = mysqli_fetch_array($result))
{
    $car_brand = $row['car_brand'];

    echo"<option value='$car_brand'>$car_brand</option>";
}
echo "</select>";
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM