无法让AJAX生成正确的值

时间:2016-05-15 03:10:04

标签: javascript php jquery ajax

尝试根据用户选择中的选择生成值列表,AJAX应根据第一个选择字段中的用户选择生成第二个列表。

表单是

<form id="fitment_search" action="wheels_search_results.php" method="GET">
    <div class="heading">
        <h4>Car search</h4>
    </div>
    <div class="form-group">Vehicle Make:
        <select name="car_make"  onchange="get_model(this.value)">
            <option value="width">SELECT</option>
                <?php while($row=mysqli_fetch_assoc($makeResult)) : ?>
                    <option value="<?php echo $row['make']; ?>"><?php echo $row['make']; ?></option>
                <?php endwhile; ?>
        </select>
    </div>
    <div class="form-group">Vehicle Model:
        <select name="car_model" id="fitment_model">
            <option>Any</option>
        </select>
    </div>
    <div class="form-group">Wheel Diam:
        <select name="diam">
            <option value="diam">SELECT</option>
        <?php mysqli_data_seek($diamQuery, 0); ?>
            <?php while($row=mysqli_fetch_assoc($diamQuery)) : ?>
                <option value="<?php echo $row['diam']; ?>"><?php echo $row['diam']; ?></option>
        <?php endwhile; ?>
        </select>
    </div>
    <button type="submit" name="fitment_button" value="" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Search Now</button>
</form>

AJAX

    <script type="text/javascript">
        function get_model(str){
            var xmlhttp;
            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("fitment_model").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","fitment_models.php?make="+str,true);
            xmlhttp.send();
        }
    </script>   

称为

的装备模型
<?php

require_once(dirname(__FILE__) . "/core/init.php");

$make=$_GET["make"];

#$sql2 = "SELECT distinct model FROM fitment WHERE make='".$make."' ORDER BY model ASC";
$sql2 = "SELECT model, yr FROM fitment WHERE make='".$make."' ORDER BY model ASC";
$result = mysqli_query($sql2); ?>
<select name="models"> <?php
while($row = mysqli_fetch_array($result))  { ?>
    <option value="<?php echo $row['model'] ?>:<?php echo $row['yr'] ?>"><?php echo $row['model'] ?> <?php echo $row['yr'] ?></option><?php
} ?>
</select>

所以基本上它应该根据表单中选择的make从DB生成一个模型列表,目前make正在运行,diam正在运行,但模型没有显示任何信息。

enter image description here

0 个答案:

没有答案