简体   繁体   English

我没有收到来自ajax请求的回复

[英]I am not getting response from ajax request

I am getting no response from ajax request . 我没有收到来自ajax请求的响应。 i am posting a ajax call to processor.php file and the processor.php file is processing the file and sending the processed file back to javascript i am gerring my result in my console log but it is not showing in the html. 我正在将ajax调用发布到processor.php文件,并且processor.php文件正在处理该文件并将已处理的文件发送回javascript,我在控制台日志中产生了我的搜索结果,但未在html中显示。 My javascript code is : 我的JavaScript代码是:

function add_to_cart(item_name,item_price){
  $('.user-info').slideUp('1200');
  $('.cart-status').slideDown('1200');

  var dataString = "item_name=" + item_name + "&item_price=" + item_price + "&page=add_to_cart";
  $.ajax({
    type: "POST",
    url: "php/processor/processor.php",
    data:dataString,
    beforeSend:function(){
      $(".cart-show-product").html('<h3>Your Cart Status</h3><img src="images/loading.gif" align="absmiddle" alt="Loading...." class="center" /><br><p class="center">Please Wait...</p>');
    },
    success: function(response){
      console.log(response);
      $(".cart-show-products").html(response);
    }
  });
}

and my php is : 和我的PHP是:

if(isset($_POST['item_name']) && !empty($_POST['item_name']) && isset($_POST['item_price']) && !empty($_POST['item_price']))
{
  $sql = mysqli_query($conn, 
      'SELECT * FROM 
        products_added 
        where 
        username = "'.mysqli_real_escape_string($conn, $_SERVER['REMOTE_ADDR']).'" 
        and 
        item_added="'.mysqli_real_escape_string($conn, $_POST['item_name']).'"'
);
if(mysqli_num_rows($sql) < 1)
{
  mysqli_query($conn, 
        "INSERT INTO products_added values(
          '', 
          '".mysqli_real_escape_string($conn, $_SERVER['REMOTE_ADDR'])."', 
          '".mysqli_real_escape_string($conn, $_POST['item_name'])."', 
          '".mysqli_real_escape_string($conn, $_POST['item_price'])."', 
          '".mysqli_real_escape_string($conn, '1')."', 
          '".mysqli_real_escape_string($conn, $_POST['item_price'])."'
          '".mysqli_real_escape_string($conn, date("d-m-Y"))."')"
  );
?>
<table class="cart-show-products">
  <thead>
    <tr>
      <td>Sl.No</td>
      <td>Item</td>
      <td>Qty</td>
      <td>Price</td>
      <td>Action</td>
    </tr>
  </thead>
<tbody>
<?php
  $sl_no = 1;
  $sql = mysqli_query(
          $conn, 
          'SELECT sum(amount) as grandtotal
                FROM products_added
                WHERE username = "'.mysqli_real_escape_string($conn, $_SERVER['REMOTE_ADDR']).'"
                ORDER BY id'
  );
  $row = mysqli_fetch_array($sql);
  $grandtotal = strip_tags($row['grandtotal']);
  $sql = mysqli_query(
                $conn, 
                'SELECT 
                id,
                item_added,
                price,
                quantity
                FROM products_added
                WHERE username = "'.mysqli_real_escape_string($conn, $_SERVER['REMOTE_ADDR']).'"
                ORDER BY id'
  );
  $row = mysqli_fetch_array($sql);
  $item_id = strip_tags($row['item_id']);
  $item_name = strip_tags($row['item_added']);
  $item_price = strip_tags($row['price']);
  $quantity = strip_tags($row['price']);
  ?>
    <tr class="items_wrap items_wrap<?php echo $item_id; ?>">
      <td><?php echo $sl_no++; ?></td>
      <td><?php echo $item_name ?></td>
      <td><?php echo $quantity ?></td>
      <td><?php echo $item_price ?></td>
      <td><a href="javascript:void(0);" class="remove-from-cart" onclick="remove_this_item('<?php echo $item_id; ?>')"><i class="fa fa-times"></i></a></td>
    </tr>
  </tbody>
</table>
<?php
}

If you're getting a response in the console, then the issue must be with your HTML. 如果您在控制台中得到响应,则问题一定出在您的HTML上。 I think part of the problem is you've created a section on the HTML page wiht a class that is the same as the table the AJAX call is bringing into the page. 我认为部分问题是您在HTML页面上创建了一个类,该类与AJAX调用将其带入页面的表相同。 I would suggest changing the HTML element to us an ID instead. 我建议改为将HTML元素更改为我们的ID。 Something like 就像是

<div id="products-table"></div>

And then change your JavaScript to 然后将您的JavaScript更改为

function add_to_cart(item_name,item_price){
  $('.user-info').slideUp('1200');
  $('.cart-status').slideDown('1200');

  var dataString = "item_name=" + item_name + "&item_price=" + item_price + "&page=add_to_cart";
  $.ajax({
    type: "POST",
    url: "php/processor/processor.php",
    data:dataString,
    beforeSend:function(){
      $("#products-table").html('<h3>Your Cart Status</h3><img src="images/loading.gif" align="absmiddle" alt="Loading...." class="center" /><br><p class="center">Please Wait...</p>');
    },
    success: function(response){
      console.log(response);
      $("#products-table").html(response);
    }
  });
}

If you stay with the class names you've used, subsequent updates are going to have problems because you'll have 2 elements on the page with the same class. 如果您保持使用的类名,则后续更新将出现问题,因为页面上将有两个具有相同类的元素。 Your script could potentially be confused about which one to change. 您的脚本可能会困惑于要更改哪个脚本。

If this is your actual code, then you have a syntax error in your PHP file. 如果这是您的实际代码,则您的PHP文件中有语法错误。 There are a missing close bracket for: 缺少以下括号:

if(isset($_POST['item_name']) && !empty($_POST['item_name']) && isset($_POST['item_price']) && !empty($_POST['item_price']))

The second problem is, you are not print anything, if this condition has failed. 第二个问题是,如果此条件失败,则您什么都不打印。

Note 注意

You do not need to use isset , if you are checking empty . 如果检查为empty ,则无需使用isset empty will return false, if the variable not set. 如果未设置变量,则empty将返回false。

You can debug your respons by check NET tab in your web developer tools, or see, what is the response of the AJAX. 您可以通过在Web开发人员工具中选中NET选项卡来调试响应,或者查看AJAX的响应是什么。

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

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