Javascript 已收到Ajax状态,但未应用后续步骤

Javascript 已收到Ajax状态,但未应用后续步骤,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个评论系统,用户在其中发表评论,并通过ajax验证数据并发送到.php页面。问题是它接收到status=1,但如果Ajax代码,则不应用else。我被困在这里了。任何建议或帮助都将受到高度重视 AJAX <script type="text/javascript"> $(document).ready(function() { $("#submit_comment").click(function() { var proceed

我有一个评论系统,用户在其中发表评论,并通过ajax验证数据并发送到.php页面。问题是它接收到status=1,但如果Ajax代码,则不应用
else。我被困在这里了。任何建议或帮助都将受到高度重视

AJAX

<script type="text/javascript">
    $(document).ready(function() {
        $("#submit_comment").click(function() {

            var proceed = true;

            $(" #comment_form textarea[required=true]").each(function(){
                $(this).css('border-color','');
                if(!$.trim($(this).val())){ //if this field is empty
                    $(this).css('border-color','red'); //change border color to red
                    proceed = false; //set do not proceed flag
                }


            });

            if(proceed)
                post_data = {
                    'user_email'        : $('input[name=email]').val(),
                    'pid'       : $('input[name=productid]').val(),
                     'msg'          : $('textarea[name=comment]').val()
                };


            $.post('comments.php', post_data, function(response){
                if(response.type == 'error'){ //load json data from server and output message
                    output = '<div class="error">'+response.text+'</div>';

                }
                else if(response.status && response.type != 'error')
                {
                    output = '<div class="success">'+response.text+'</div>';
                    $(response.html).hide().insertBefore('#comment_form').slideDown();

                    $(" #comment_form textarea[required=true]").val('');
                    $("#comment_form #comment_body").slideUp();
                }
                $("#comment_form #comment_results").hide().html(output).slideDown();
            }, 'json');
        });


        //reset previously set border colors and hide all message on .keyup()
        $("#comment_form  input[required=true], #comment_form textarea[required=true]").keyup(function() {
            $(this).css('border-color','');
            $("#result").slideUp();
        });
    });
</script>

$(文档).ready(函数(){
$(“#提交#评论”)。单击(函数(){
var=true;
$(“#注释_表单textarea[required=true]”)。每个(函数(){
$(this.css('border-color','');
if(!$.trim($(this.val()){//如果此字段为空
$(this.css('border-color','red');//将边框颜色更改为红色
继续=false;//设置不继续标志
}
});
如果(继续)
post_数据={
'user_email':$('input[name=email]')。val(),
'pid':$('input[name=productid]')。val(),
'msg':$('textarea[name=comment]).val()
};
$.post('comments.php',post_数据,函数(响应){
if(response.type=='error'){//从服务器加载json数据并输出消息
输出=''+响应。文本+'';
}
else if(response.status&&response.type!=“error”)
{
输出=''+响应。文本+'';
$(response.html).hide();
$(“#注释_表单textarea[required=true]”)。val(“”);
$(“#注释表#注释正文”).slideUp();
}
$(“#注释表单#注释结果”).hide().html(输出).slideDown();
}“json”);
});
//重置以前设置的边框颜色并隐藏.keyup()上的所有消息
$(“#注释(表单输入[required=true],#注释)表单文本区域[required=true]”).keyup(函数(){
$(this.css('border-color','');
$(“#结果”).slideUp();
});
});
表格

<?php
   include "comment.php";
   $comments = array();
   $result = mysqli_query($con,"SELECT * FROM comments where product_id='$id'  ORDER BY dt LIMIT 5");
  while($row = mysqli_fetch_assoc($result))
   {
   $comments[] = new Comment($row);
  }
    ?>
  <?php
  foreach($comments as $c){
   echo $c->markup();
     }
 ?>
  </div>
          <?php

        }
        }


        ?>
  <div class="form-style" id="comment_form">

                <div id="comment_results"></div>
                <div id="comment_body">

                        <input type="hidden" name="email" id="email" value="<?php echo $email?>">
                         <input type="hidden" name="productid" id="productid" value="<?php echo $pid?>" />
                         <label for="field5"><span>Comment: <span class="required">*</span></span>
                        <textarea name="comment" id="comment" class="textarea-field" required="true"></textarea>
                    </label>
                    <label>
                        <span>&nbsp;</span><input type="submit" id="submit_comment" value="Submit"">
                    </label>
                </div>
            </div>

你说的是这一行吗?response.status&&response.type!='错误'@Glubus-yes。这行后面的代码无效。您是否尝试删除该谓词的第二部分?由于您要在if中检查“error”的相等性,因此在else if中进行检查是多余的。另外,当您执行console.log(response.type)时,会记录哪些内容;(在ifs之前,我尝试了这个`$.post('comments.php',post_数据,函数(response){console.log(response.type)}`但是没有打印。你能告诉我有什么吗?
<?php

class Comment
{
    private $data = array();

    public function __construct($row)
    {
    $this->data = $row;
    }

    public function markup()
    {     $d = &$this->data;

    // Converting the time to a UNIX timestamp:
        $d['dt'] = strtotime($d['dt']);

        // Needed for the default gravatar image:
  return '
    <div class="comment">
    <div class="name">'.$d['email'].'</div>
    <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
                <p>'.$d['body'].'</p>
            </div>
        ';
    }
    }

   ?>
<?php
include("db/db.php");
include "comment.php";
if($_POST)
{

    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        $output = json_encode(array( //create JSON data
            'type'=>'error',
            'text' => 'Sorry Request must be Ajax POST'
        ));
        die($output); //exit script outputting json data
    }

    //Sanitize input data using PHP filter_var().
    $user_name      = filter_var($_POST["user_email"], FILTER_SANITIZE_STRING);
    $pid        = filter_var($_POST["pid"], FILTER_VALIDATE_INT);
    $message        = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
    $arr = array();
    //additional php validation

    if(strlen($message)<3){ //check emtpy message
        $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
        die($output);
    }

    mysqli_query($con,"INSERT INTO comments(email,body,product_id) values('$user_name','$message','$pid')");

    $arr['dt'] = date('r',time());
    $arr['id'] = mysql_insert_id();
    $res=mysqli_query($con,$query);
    $arr = array_map('stripslashes',$arr);

    $insertedComment = new Comment($arr);

    if(!$res)
    {

        $output = json_encode(array('type'=>'error', 'text' => 'Cannot recieve your comment.'));
        die($output);
    }else{


       $output= json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your review','status'=>1,'html'=>$insertedComment->markup()));
echo $output;
        die($output);
    }
}
?>