1

I am trying to submit form to database using ajax. I have sucessfully managed to post to database using AJAX. But not the trouble is I am not sure how to check if the submitted value already exist in database.

I already have php that does so, But I am trying to use AJAX. I am using jquery validate plugin Following is the code.

$('#subscribeForm').validate({
  errorElement: "p",
  errorPlacement: function(error, element) {
  $('.status').html(error);
},

rules: {
  email: {
    required: true,
    email: true,
    remote: {
        url: 'index.php',
        type: 'post',
        data: {
            email: function(){
            // not sure how to check this
        }
    }
}
}
},

messages: {
email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
// remote: jQuery.format("{0} is already in use")  // doesnt work
}
},

submitHandler: function(form) {
var btn = $(".submit_button");
btn.attr("value", "Submitting..");

$(form).ajaxSubmit({
    success: function(){
        btn.attr("value", "Submitted");
    }
});
return false;
}
});

This is my PHP:

if(isset($_REQUEST['subscribeForm'])){
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'Please provide valid email address.';
} else {
    if(email_exist($email)){
        $status = 'You are already subscribed.';
    } else {
        add_email($email);
        $status = 'Thank you for subscribing.';
    }
   }
 } 

EDIT

This is what I have tried but again with fail.

if(isset($_REQUEST['subscribeForm'])){
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'Please provide valid email address.';
} else {
    if(email_exist($email)){
        $status = 'You are already subscribed.';
        echo false;
    } else {
        add_email($email);
        $status = 'Thank you for subscribing.';
        echo true;
    }
}
} 

And my Javascript

rules: {
email: {
required: true,
email: true,
remote: {
  url: 'index.php',
  type: 'post'
   }
}
},

messages: {
 email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
remote: "You are already subscribed."
   }
 },

2 Answers 2

1

You should do something in your JS to do success function on return of "Thank you for subscribing" echo.

Try;

Javascript

messages: {
email: {
required: "Please enter your email.",
email: "Please enter a valid email.",
success: "Thank you for subscribing.",
// remote: jQuery.format("{0} is already in use")  // doesnt work
}
},

PHP

if(isset($_REQUEST['subscribeForm'])){
$email = trim($_POST['email']);

if(empty($email) || !valid_email($email)){
    $status = 'false';
} else {
    if(email_exist($email)){
        $status = 'false';
    } else {
        add_email($email);
        $status = 'true';
    }
   }
 }
echo $status;
Sign up to request clarification or add additional context in comments.

1 Comment

I am not concerned with the message. I am stuck on how to check if the email already exist.
1

Simply echo an error code in your PHP file and use AJAX from the Javascript to parse the error code and determine whether or not the email has been registered.

PHP:

define('ERROR_SUCCESS', 0);
define('ERROR_ALREADY', 1);
define('ERROR_INVALID', 2);

if (isset($_REQUEST['subscribeForm'])) {
    $email = trim($_POST['email']);

    if (empty($email) || !valid_email($email)) {
        $status = 'Please provide valid email address.';
        echo ERROR_INVALID;
    } else {
        if (email_exist($email)) {
            $status = 'You are already subscribed.';
            echo ERROR_ALREADY;
        } else {
            add_email($email);
            $status = 'Thank you for subscribing.';
            echo ERROR_SUCCESS;
        }
    }
}

1 Comment

I am fine with the PHP part. javascript is where I am stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.