Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX:如何从php数据成功或错误消息比较AJAX中的变量_Php_Jquery_Ajax - Fatal编程技术网

AJAX:如何从php数据成功或错误消息比较AJAX中的变量

AJAX:如何从php数据成功或错误消息比较AJAX中的变量,php,jquery,ajax,Php,Jquery,Ajax,我尝试了stackoverflow.com上的许多例子 我从这个示例中选择代码 但总是给我错误 每当我运行下面的代码时,如果ajax代码出错,那么ajax会显示错误消息,但当php有错误代码时,ajax总是显示成功:函数数据代码 据我所知,data.status=='success'没有从php进行比较,因此我的代码给出了错误 我知道这个问题是重复的,但我看到了stackoverflow的所有问题,这些问题并没有解决我的问题,所以我再次发布 请帮忙 AJAX代码 $document.read

我尝试了stackoverflow.com上的许多例子

我从这个示例中选择代码

但总是给我错误

每当我运行下面的代码时,如果ajax代码出错,那么ajax会显示错误消息,但当php有错误代码时,ajax总是显示成功:函数数据代码

据我所知,data.status=='success'没有从php进行比较,因此我的代码给出了错误

我知道这个问题是重复的,但我看到了stackoverflow的所有问题,这些问题并没有解决我的问题,所以我再次发布

请帮忙

AJAX代码

$document.readyfunction{ $function{ $'.contactf'.submitfunction e{ e、 防止违约; 如果!联系有效 返回false; $this.find:submit.propdisabled,true; $'gif'.css'display','block'; var form=$this; var post_url='contactmail1.php'; var post_data=form.serialize; $.ajax{ 键入:“POST”, url:post_url, 数据:post_数据, 成功:函数数据{ 如果data.status==“成功”{ 警报数据; }如果data.status=='error'{ 警报数据; } }, 错误:函数数据{ 警报“客户端失败”; } }; }; }; }; PHP


更改Java脚本代码-添加数据类型:json


并删除已提交的echo查询;echo json_编码$response_数组后

删除已提交的回显查询;echo json_编码$response_数组后;不要使用截图,请将代码复制到问题中,JSON编码部分字段为空;或者使用$response_数组['status']='blank';并将dataType:json添加到post中,您必须对整个响应进行编码,而不仅仅是其中的一部分,否则返回到客户端的最终结果将不是有效的json。当前通过echo直接输出的字符串需要合并到编码对象中,如果它们是多余的,则需要删除。您没有说它给您带来了什么错误,但我猜是关于无效JSON的;打印响应。
<?php

//
header('Content-type: application/json');

if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {

    require 'phpmailer/PHPMailerAutoload.php';
    $name = $_POST[ 'Name' ];
    $email = $_POST[ 'Email' ];
    $phone = $_POST[ 'number' ];
    $sub = $_POST[ 'contact_subject' ];
    $msg = $_POST[ 'Message' ];


    if ( !( isset( $_POST[ 'Name' ] )and isset( $_POST[ 'Email' ] )and isset( $_POST[ 'number' ] )and isset( $_POST[ 'contact_subject' ] )and isset( $_POST[ 'Message' ] ) ) ) {
        echo "Some Field is Blank";
    } else {


        $mail = new PHPMailer;

        $mail->setFrom( $email, $name );


        $mail->addAddress( 'mail@gmail.com', 'Inderjeet' );

        $mail->Subject = 'Mail from site';

        $mail->msgHTML( '<html><body><table border=0 width=554><tr><td colspan=2><p><b>Enquiry from Contact Page Thorsoncne.com</b><br><br></p></td></tr><tr><td colspan=2 class=text4>FORM submitted at ' . date( 'd F Y h:i:s A' ) . '<br></td></tr>   
<tr><td width=200 class=text3>Name :</td><td class=text3>' . $name . '</td></tr>   
<tr><td>Email Id :</td><td>' . $email . '</td></tr>   
<tr><td>Phone/Mobile :</td><td>' . $phone . '</td></tr>   
<tr><td>Subject :</td><td>' . $sub . '</td></tr>   
<tr><td>Message :</td><td>' . $msg . '</td></tr>   
</table></body></html>' );

        if ( !$mail->send() ) {


            $response_array['status'] = 'failed';
            echo "Mailer Error: " . $mail->ErrorInfo;
            echo json_encode($response_array);

        } else {
            $response_array['status'] = 'success';
            echo json_encode($response_array);
            echo "Query Submitted";

        }
    }
}
$(document).ready(function() {
  $(function() {
    $('.contactf').submit(function(e) {
      e.preventDefault();
      if (!contactvalid()) return false;
      $(this).find(":submit").prop("disabled", true);
      $('#gif').css('display', 'block');
      var form = $(this);
      var post_url = 'contactmail1.php';
      var post_data = form.serialize();
      $.ajax({
        type: 'POST',
        url: post_url,
        dataType: "json",
        data: post_data,
        success: function(data) {
          if (data.status == 'success') {
            alert(data);
          } else if (data.status == 'error') {
            alert(data);
          }
        },
        error: function(data) {
          alert('failed client side');
        }
      });
    });
  });
});