Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP AJAX Responses, how can i add different conditions?
    primarykey
    data
    text
    <p>I want to be able to set the following:</p> <p>1) If the email already exists to return an error 2) If successful to return an error 3) if error to return error</p> <p>At the moment it works, but allows you to add same email address and sends successful response but need to add one for existing email</p> <pre><code>$('form').submit(function(){ // check if passwords match; you might want to do more thorough validation var hasError = false; var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; var emailaddressVal = $("#email").val(); if(emailaddressVal == '') { $("#email").after('&lt;span class="error"&gt;Please enter your email address.&lt;/span&gt;'); hasError = true; } else if(!emailReg.test(emailaddressVal)) { $("#email").after('&lt;span class="error"&gt;Enter a valid email address.&lt;/span&gt;'); hasError = true; } else if(hasError == false) { // make ajax post request and store the response in "response" variable $.post('submit.php', $(this).serialize(), function(response){ // process response here (assume JSON object has boolean property "ok" if(response.ok==true){ // sweet, it worked! alert('OK!'); }else{ // handle error alert('Ooops'); } }, 'json'); } // stop the form from being submitted return false; }); </code></pre> <p>And the php is:</p> <pre><code>&lt;?php ini_set('display_errors', 1); error_reporting(E_ALL); $con = mysql_connect("localhost","root",""); //Replace with your actual MySQL DB Username and Password if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("table", $con); //Replace with your MySQL DB Name $first_name=mysql_real_escape_string($_POST['firstname']); $last_name=mysql_real_escape_string($_POST['lastname']); $email=mysql_real_escape_string($_POST['email']); $sql="INSERT INTO email_list (first_name,last_name,email) VALUES ('$first_name','$last_name','$email')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "The form data was successfully added to your database."; mysql_close($con); ?&gt; </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload