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..!

You can check your server side sript if it works OK. Try to input the URL with the parameters into your browser directly e.g.:

php-includes/news_letter_send.php?name=somename&email_address=justme@example.com

You will have to change the global array to $_GET temporarily:

if(isset($_GET['name']) && isset($_GET['email_address'])) {
    $name = $_GET['name'];
    $email = $_GET['email_address'];
        ...
} else {
    die 'failed';
}

You can use different parameters in the query string: the ones that exist in the databse and the ones that don't. The script should echo either duplicate, sent or failed. If this is true your backend script works OK. In this case go and check the jquery ajax code, i.e if the msg gets alerted OK.

Errors could be also in the include file or in the success element.

And one more important thing: you should sanitize the user supplied data (and check for existence of $_POST elements earlier):

if(isset($_POST['name']) && isset($_POST['email_address'])) {
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email_address']);
    $sql = "SELECT tbl_news_letters.emailaddress WHERE tbl_news_letters.emailaddress=$email";
    ...

} else {
    die 'failed';
}.

And: drop the mysql database extension and switch to PDO.

thank you broj1!

Lately Ive found out that the only problem is with my server side script...Thanks for the advised..I had also implemented sanitation on POST...tnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.