Reputation: 1400
I want to be able to set the following:
1) If the email already exists to return an error 2) If successful to return an error 3) if error to return error
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
$('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('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if(!emailReg.test(emailaddressVal)) {
$("#email").after('<span class="error">Enter a valid email address.</span>');
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;
});
And the php is:
<?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);
?>
Thanks!
Upvotes: 0
Views: 255
Reputation: 482
jquery
$('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('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if(!emailReg.test(emailaddressVal)) {
$("#email").after('<span class="error">Enter a valid email address.</span>');
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=='0'){
alert('required fields empty');
}else if(response.ok=='1'){
alert('email already exists');
}
else if(response.ok=='2')
{
alert('thankyou for your input');
}
}, 'json');
}
// stop the form from being submitted
return false;
});
php code
<?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']);
if(empty($first_name) || empty($last_name) || empty($email) ) {
echo json_encode( array('ok'=> '0' ) );
exit();
}
$sql="Select * from email_list where email='".$email."' ";
$sqll=mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$data=mysql_fetch_array($sqll);
if($data['email']) {
echo json_encode( array('ok'=> '1' ) );
exit();
}
$sql="INSERT INTO email_list (first_name,last_name,email) VALUES ('$first_name','$last_name','$email')";
mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$value = mysql_insert_id() > 0;
if($value)
echo json_encode( array('ok'=> '2' ) );
mysql_close($con);
exit();
?>
Upvotes: 1
Reputation: 429
Just check for the email in the db before u add. Hope This Helps.
<?php
$first_name=mysql_real_escape_string($_POST['firstname']);
$last_name=mysql_real_escape_string($_POST['lastname']);
$email=mysql_real_escape_string($_POST['email']);
$sql = "SELECT * FROM email_list WHERE `email`='$email'";
$res= @mysql_query($sql);
if(@mysql_num_rows($res)>0)
{
echo "Email Already Exists" ;
}
else
{
$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.";
}
?>
Upvotes: 2
Reputation: 19407
$sql="SELECT email FROM email_list WHERE email = '$email'";
$result = mysql_query($sql, $con) or die('Error: ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
// Error - Email already exists
echo "Error: The email address already exists.";
} else {
$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);
I have added a check to see if the email address already exists, and output an error if it does. There are also error outputs for mysql errors.
If you need the output to be formatted in a certain way, use JSON. But the above should get you started.
Upvotes: 2
Reputation:
just add following line in end of you php file
$value = mysql_insert_id() > 0;
echo json_encode( array('ok'=> $value ) );
Upvotes: 1