AJAX PHP输出显示不正确

AJAX PHP output not showing correctly

本文关键字:不正确 显示 输出 PHP AJAX      更新时间:2023-09-26

AJAX脚本成功地将数据发送给PHP脚本,然后在不刷新页面的情况下将值插入数据库。然而,问题是,在成功时,值不会从PHP输出到页面(错误输出也可以)。表单上只写着"张贴……"’,并暂停,直到您刷新它并从数据库中显示该值。

下面是我的JS代码:
$(document).ready(function(){
  var form = $('form');
  var submit = $('#submit');
  form.on('submit', function(e) {
      e.preventDefault();
      $.ajax({
          url: 'ajax_comment.php',
          type: 'POST',
          cache: false,
          data: form.serialize(),
          beforeSend: function(){
              submit.val('Posting...').attr('disabled', 'disabled');
          },
          success: function(data){
            var data_code = data.substring(0,3);
            var return_message = data.substring(3); // this is return message without code
            if(data_code == 100) { 
              var item = $(return_message).hide().fadeIn(800);
              $('.new-comment').append(item);
              form.trigger('reset');
              submit.val('Submit Comment').removeAttr('disabled');
            } else if(data_code == 200) {
              //its a fail
              alert("Error: " + return_message);
            }
         }
      });
  });
});

这是AJAX发送数据到的页面:

    if(empty($order_id) === true || empty($comment) === true) {
    echo "200comment or order id is empty";
    exit();
} else if($num_rows_reviewed> 0) {
    echo "200";
    exit();
} elseif($no_id_match == 0) {
    echo "200";
    exit();
} elseif(strlen($comment) > 499) {
   echo "200 comment cannot be bigger then 499";
    exit();
} else {
   echo"100"; // all is good
   ?>  <div class="comment-item">
         <div class="comment-post">
           <h3><?php echo $name; ?>: <span><?php echo $date; ?></span></h3>
           <p><?php echo $comment; ?></p>
         </div>
       </div>
   <?php }

我真的很挣扎,我不知道从这里去哪里!这是如此接近工作,但我不知道如何解决它。错误输出消息很好地传递过来——只有成功消息和数据没有按要求传递过来。

提前感谢!


编辑
Uncaught Error: Syntax error, unrecognized expression: 100this    <div class="comment-item">
  <div class="comment-post">
    <h3>Andrew D: <span>17th March 2014</span></h3>
    <p>hi</p>
  </div>
</div> 

EDIT2

100this    <div class="comment-item">
  <div class="comment-post">
    <h3>Andrew D: <span>17th March 2014</span></h3>
    <p>hi</p>
  </div>
</div>

我认为你需要一个明确的回应…所以,你的错误:

  1. 您没有为ajax请求提供数据类型,为了防止不必要的解析,您应该使用它。
  2. 如果你正在使用ajax,你应该使用JSON来返回数据,而不是用字符串操作解码数据
  3. 不能在不存在的html元素上使用动画
  4. 你的php文件中有你的错误。您可以看到浏览器从php脚本接收到的注销文本。您应该检查php文件的错误,在echo "100"和div标记之间。可能您没有从php文件中复制所有内容

所以你应该试试下面的方法!

PHP

$response = new stdClass();
$response->code = 200;
$repsonse->message = "";
if (empty($order_id) === true || empty($comment) === true) {
    $repsonse->message = "comment or order id is empty";
} else if ($num_rows_reviewed > 0) {
} else if ($no_id_match == 0) {
} else if (strlen($comment) > 499) {
    $repsonse->message = "comment cannot be bigger then 499";
} else {
    $response->code = 100;
    $repsonse->message = "<div class='"comment-item newItem'" style='"display: none;'">
        <div class='"comment-post'">
            <h3>".$name.": <span>".$date."</span></h3>
            <p>".$comment."</p>
         </div>
         </div>";
}
echo json_encode($response, JSON_NUMERIC_CHECK);

JAVASCRIPT AJAX请求

$.ajax({
      url: 'ajax_comment.php',
      type: 'POST',
      cache: false,
      dataType: 'json',
      data: form.serialize(),
      beforeSend: function(){
          submit.val('Posting...').attr('disabled', 'disabled');
      },
      success: function(data){
        if(data.code === 100 ) { 
          $('.new-comment').append(data.message);
          $('.new-comment div.newItem').fadeIn(800).removeClass('newItem');
          form.trigger('reset');
          submit.val('Submit Comment').removeAttr('disabled');
        } else if(data.code == 200) {
          //its a fail
          alert("Error: " + data.message);
        }
     }
});

对不起,我写错了,试试新代码。

问候,hotzu