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

时间:2016-02-03 12:56:56

标签: javascript php jquery ajax

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

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>

表格

<?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>

comment.php

<?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>
        ';
    }
    }

   ?>

的comments.php

<?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);
    }
}
?>

0 个答案:

没有答案