Created
April 29, 2015 14:42
-
-
Save stramargio/eb5510e2c0853f89fd8b to your computer and use it in GitHub Desktop.
php ajax email register
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($_GET['action'] == 'signup'){ | |
mysql_connect('localhost','root','andrea'); | |
mysql_select_db('ilmiodb'); | |
//sanitize data | |
$email = mysql_real_escape_string($_POST['signup-email']); | |
$privacy = mysql_real_escape_string($_POST['signup-privacy']); | |
$ricontatta = mysql_real_escape_string($_POST['signup-ricontatta']); | |
//validate email address - check if input was empty | |
if (empty($email)){ | |
$status = "error"; | |
$message = "Non è un indirizzo email."; | |
} | |
else if (!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address | |
$status = "error"; | |
$message = "Non è un indirizzo email valido."; | |
} | |
else if (empty($privacy)) { | |
$status = "error"; | |
$message = "Devi accettare la Privacy Policy."; | |
} | |
else { | |
$existingSignup = mysql_query("SELECT * FROM signups WHERE signup_email_address='$email'"); | |
if (mysql_num_rows($existingSignup) < 1) { | |
$date = date('Y-m-d'); | |
$time = date('H:i:s'); | |
$insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time, signup_privacy, signup_ricontatta) VALUES ('$email','$date','$time', '$privacy', '$ricontatta')"); | |
if($insertSignup){ | |
$status = "success"; | |
$message = "Registrazione avvenuta con successo."; | |
} | |
else { | |
$status = "error"; | |
$message = "Errore tecnico: riprovare più tardi."; | |
} | |
} | |
else { | |
$status = "error"; | |
$message = "Questa email è già registrata."; | |
} | |
} | |
//return json response | |
$data = array( | |
'status' => $status, | |
'message' => $message | |
); | |
echo json_encode($data); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment