简体   繁体   English

PHP Ajax依赖下拉列表不起作用

[英]PHP Ajax dependent dropdown not working

I am trying to implement dependent dropdown using ajax and php. 我正在尝试使用ajax和php实现依赖下拉列表。 But anyhow the response from second dropdown is not coming. 但是无论如何,第二个下拉列表的响应不会到来。 after checking in console, I am able to see the id of first dropdown but no response from second dropdown. 在控制台中签入后,我可以看到第一个下拉列表的ID,但是第二个下拉列表没有任何响应。 So, When i click on first dropdown, I just get a blank value in Second dropdown. 因此,当我单击“第一个”下拉列表时,我在“第二个”下拉列表中仅得到一个空白值。

HTML Code HTML代码

 <div class="col-md-6">
                                                    <?php
                                                    require_once('db.php');
                                                    $varient_result = $conn->query('select * from tv_varient');
                                                    ?>
                                                    <select name="varient" id="varient-list" class="form-control  c-square c-theme" required>
                                                        <option value="">Select Varient</option>
                                                        <?php
                                                            if ($varient_result->num_rows > 0) {
                                                            // output data of each row
                                                            while($row = $varient_result->fetch_assoc()) {
                                                        ?>
                                                        <option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
                                                        <?php
                                                        }
                                                        }
                                                        ?>
                                                    </select>
                                                 </div>    
                                                    </br></br>

                                                        <label for="inputPassword3" class="col-md-4 control-label" style="margin-left: 15px;">Price:</label>
                                                            <div class="col-md-6">
                                                                <select name="price" id="price-list" class="form-control  c-square c-theme" required>
                                                                    <option value=''>Select Price *</option>
                                                                </select>
                                                            <div>
                                                                </div>
                                                       </div>
                                                  </div>
                                                    </div> 
                                                </div>

AJAX Code
                                                         <script>
                                                                $('#varient-list').on('change', function(){
                                                                 var varient_id = this.value;
                                                                 $.ajax({
                                                                 type: "POST",
                                                                 url: "get_price.php",
                                                                 data:'varient_id='+varient_id,
                                                                 success: function(result){
                                                                 $("#price-list").html(result);
                                                                  }
                                                                 });
                                                                });
                                                            </script>

get_price.php get_price.php

?php
require_once('db.php');
//$country_id = mysqli_real_escape_string($_POST['country_id']);
//var_dump($country_id);
//var_dump($_POST['country_id']);
$varient_id = $_POST['veriant_id'];
echo $varient_id;
//var_dump($varient_id);
var_dump($_POST['varient_id']);
if($varient_id!='')
{
    $states_result = $conn->query('select * from tv_price where veriant_id='.$varient_id.'');
    $options = "<option value=''>Select Price</option>";
    while($row = $states_result->fetch_assoc()) {

    $options .= "<option value='".$row['price']."'>".$row['price']."</option>";
    }
    echo $options;
}

?>

Try below code, 试试下面的代码,

 $(document).on('change', '#varient-list', function(e) { var varient_id = this.value; $.ajax({ type: "POST", url: "get_price.php", data:'varient_id='+varient_id, success: function(result){ $("#price-list").html(result); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

varient_id & veriant_id on the POST is wrong (the e & a difference). POST上的varient_id和veriant_id错误(e和差异)。

$varient_id = $_POST['veriant_id']; $ varient_id = $ _POST ['veriant_id']; should be $varient_id = $_POST['varient_id']; 应该是$ varient_id = $ _POST ['varient_id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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