Good day!
I have a news letter form and ajax validation on empty fields is working find.. But when I try to submit the form with all the required fields, I cannot get the data_html response from my php file? Maybe i missed some part that I cannot see t myself..
<Here is my Ajax Validator>
$(document).ready(function() {
$('#submit-form').click(function(){
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var name = $('#newsletter-form [name="name"]').val();
var email_address = $('#newsletter-form [name="email"]').val();
if(name == ""){
alert("Please enter your name.");
return false;
}
if(email_address == ""){
alert("Your email is required.");
return false;
}else if(reg.test(email_address) == false){
alert("Invalid Email Address.");
return false;
}
if(name != "" && reg.test(email_address) != false) {
data_html = "name=" + name + "&email_address=" + email_address;
//alert(data_html);
$.ajax({
type: 'POST',
url: 'php-includes/news_letter_send.php',
data: data_html,
success: function(msg){
alert(msg);
if (msg == 'duplicate'){
$('#success').html('<div class="error">This email address is already registered. Please use another one!</div>') ;
}else if (msg == 'sent'){
$('#success').html('<div class="success">Congratulations! You will receive email updates from us related to our citys schedules and activities. Thank you!</div>');
$('#newsletter-form [name="name"]').val('');
$('#newsletter-form[name="email"]').val('');
}else{
$('#success').html('<div class="error">Cannot send your request. Please try again later or refresh this page!</div>');
}
}
});
}
});
});
<And here is my php file to process the data>
<?php require_once('Connections/citywebDB.php'); ?>
<?php
$name = $_POST['name'];
$email = $_POST['email_address'];
if (isset($name) && isset($email)) {
$sql = "SELECT tbl_news_letters.emailaddress WHERE tbl_news_letters.emailaddress=$email";
$result = mysql_query($sql);
if($result)
{
echo 'duplicate';
}else{
$sql="INSET INTO tbl_news_letters (name, emailaddress) VALUES ($name, $email)";
$result = mysql_query($sql, $citywebDB) or die(mysql_error());
if($result)
{
echo 'sent';
}else{
echo 'failed';
}
}
}
?>
Thank you for helping..!