Inderjeet
Inderjeet

Reputation: 1528

AJAX: how to compare variable in ajax from php data success or error message

I tried many example from stackoverflow.com jQuery ajax success error

I pick code from this examle enter image description here

but always gives me error.

whenever i run below code if ajax code error then ajax shows error message but when php have error code ajax always show success: function ( data ) code.

as i know data.status == 'success' not comparing from php so my code gives error

i know this question is duplicate but i saw all questions from stackoverflow these not resolve my problem so i post again

please help

AJAX CODE

 $(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,
                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');
                }
            });
        });
    });
});

PHP

<?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( '[email protected]', '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";

        }
    }
}

Upvotes: 0

Views: 1472

Answers (1)

Sanjay Chaudhari
Sanjay Chaudhari

Reputation: 420

Change your Java script code - Add dataType: "json"

$(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');
        }
      });
    });
  });
});

And Remove echo "Query Submitted"; after echo json_encode($response_array);

Upvotes: 1

Related Questions