Jesse Elser
Jesse Elser

Reputation: 976

Ajax Error Reporting In PHP Not Using JSON

So I'm a bit confused.

I need to add an error handler to my AJAX depending on the result from PHP.

I was wondering if there was a way for me to add a variable like $success = error or $success = success in my PHP to trigger the AJAX functions.

I did some reading but everything I read involves JSON.

Here is my PHP with the $success variables where they should be, but I'm not sure where to start with the AJAX.

I'm not asking for a code to be written for me, but just some guidance.

if(isset($_POST['submit'])) {

require($_SERVER['DOCUMENT_ROOT']."/settings/functions.php");

$conn = getConnected("oversizeBoard");

if(empty($_POST['first_name'])) {
 $success = "error";
 echo "First Name Is Required.";
 exit();
 }
 else {
  $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
 }
if(empty($_POST['last_name'])) {
 $success = "error";
 echo "Last Name Is Required.";
 exit();
 }
 else {
  $last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
 }
if(empty($_POST['email'])) {
 $success = "error";
 echo "Email Is Required.";
 exit();
 }
 else {
  $email = mysqli_real_escape_string($conn, $_POST['email']);
 }

if(!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['email'])) {
$checkEmail = mysqli_query($conn, "SELECT * FROM subscriptions WHERE email='$email'");
if(mysqli_num_rows($checkEmail) > 0){
 $success = "error";
 echo "You Are Already Subscribed!";
}
else {
 if (!mysqli_query($conn,$checkEmail)) {
  $subscribeQuery = "INSERT INTO subscriptions (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
  if (mysqli_query($conn, $subscribeQuery)) {
   $success = "success";
   echo "You Have Successfully Subscribed!";
  }
  else {
   echo "Error: ".mysqli_error($conn);
  }
 }
}
mysqli_close($conn);
}
}
else {
 echo "You Are not Authorized To View This Page.";
}

And the AJAX:

    function submitForm() {
    $.ajax({type:'POST', url: 'http://example.com/form/postSubscription.php', data:$('#subscription_form').serialize(), 
    error: function(response) { // if php variable is $success = "error"
       notif({
                msg: response,
                type: "error",
                position: "center"
            });
    },
    success: function(response) { // if php variable is $success = "success"
       notif({
                msg: response,
                type: "success",
                position: "center"
            });
    }});

    return false;
}

Do I need to use JSON to accomplish this or is there another way?

Upvotes: 0

Views: 166

Answers (1)

David784
David784

Reputation: 7464

Rather than using echo "You Have Successfully Subscribed!"; etc, you probably want to create an output object/array up front, and then populate it with the data you want. Like this:

$data['text']= "You Have Successfully Subscribed!";
$data['success'] = "success";

Then you finish up with something like this:

header('Content-Type: application/json');
echo json_encode($data);

make sure you don't do any echo statements prior to the header or you'll get an error. Also, you don't want to do any echo statements other than the json_encode, or your json probably won't be parsed correctly.

On the client side, in your $.ajax, your success would work something like this:

$.ajax({ ...
  success: function(response){
    if(response.success=="success") {
      $('#output').text(response.text);
    }
  }
});

Upvotes: 2

Related Questions