简体   繁体   English

从PHP文件更改功能ajax调用

[英]change function ajax call from php file

The following code consists of drop-down "(id=name" which populates from "listplace.php" through ajax call which works correctly. 以下代码由下拉列表“(id = name)”组成,该下拉列表通过可正常工作的ajax调用从“ listplace.php”填充。

Now I am trying to make another ajax call using the change function . 现在,我尝试使用change function进行另一个ajax调用。 when I select the particular item already populated on dropdown box it has to pass the selected item name1 in 'where' query to dataprod.php and has to display the products by clearing the existing products list available. 当我选择下拉列表中已填充的特定项目时,它必须将'where'查询中的选定项目name1传递给dataprod.php并且必须通过清除现有的可用产品列表来显示产品。

I am doubtful over the $name1 response from dataprod.php . 我对dataprod.php$name1响应感到怀疑。 Please help!! 请帮忙!!

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
    $.ajax({
        type: "POST",
        data: {place: '<?= $_GET['place'] ?>'},
        url: 'listplace.php',
        dataType: 'json',
        success: function (json) {
            if (json.option.length) {
                var $el = $("#name"); 
                $el.empty(); // remove old options
                for (var i = 0; i < json.option.length; i++) {
                    $el.append($('<option>',
                        {
                            value: json.option[i],
                            text: json.option[i]
                        }));
            }else {
                alert('No data found!');
            }
        }
    });
</script>

ajax 2 阿贾克斯2

$(document).ready(function(){
 $("#name").change(function(){
 var name1 = this.value;
 $.ajax ({
     url: "dataprod.php",
     data: {place: '<?= $_GET['name1'] ?>'},
     success: function (response) {
        $('.products-wrp').html('')
        $('.products-wrp').html(response);     
     }
      }else {
        $('.products-wrp').html('');
  }
}

dataprod.php dataprod.php

<?php
include("config.inc.php");
$name1 = $_POST['name1'];
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code,  
product_image, product_price FROM products_list where product_name='$name1'");

$products_list =  '<ul id ="products_list" class="products-wrp">';
while($row = $results->fetch_assoc()) {
$products_list .= <<<EOT
<li>
<form class="form-item">
<h4>{$row["product_name"]}</h4>
<div>
<img src="images/{$row["product_image"]}" height="62" width="62">
</div>
<div>Price : {$currency} {$row["product_price"]}<div>
</form>
</li>
EOT;
}
$products_list .= '</ul></div>';
echo $products_list;
?>                 

Since you are calling ajax event on element which is loaded via ajax action so the change event is not bind and nothing is happend. 由于您正在通过ajax操作加载的元素上调用ajax事件,因此change事件不会被绑定,因此不会发生任何事情。

For ajax 2 action use below code. 对于ajax 2操作,请使用以下代码。

$(document.body).on('change',"#name",function (e) {
   //doStuff
  var name1 = this.value;
 $.ajax ({
     url: "dataprod.php",
     data: {place: '<?= $_GET['name1'] ?>'},
     success: function (response) {
        $('.products-wrp').html('')
        $('.products-wrp').html(response);     
     }
      }else {
        $('.products-wrp').html('');
  }
}

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

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