views:

21

answers:

1

I have a "Coming soon" page but i want to add an "insert your e-mail" option and the e-mail to be inserted on a csv or txt file. What i have is an ajax validate e-mail address and the input field:

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[w-s]+")|([w-]+(?:.[w-]+)*)|("[w-s]+")([w-]+(?:.[w-]+)*))(@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$)|(@[?((25[0-5].|2[0-4][0-9].|1[0-9]{2}.|[0-9]{1,2}.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2}).){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})]?$)/i);
return pattern.test(emailAddress);
}



/*FORM validation and div changing*/
$(document).ready(function() { 
    $().click(function (ev) {
        var $target = $(ev.target);
          if( !$target.is("input") ) {
                $("input#email").val('Vendosni e-mail tuaj qe tju njoftojme per hapjen e faqes');
          }
    }); 

    $("#email").click(function() {
            $("input#email").val('');
    });

    $("#submit").click(function() {
        var email = $("input#email").val();
        if(!isValidEmailAddress(email)){ 
            $("input#email").focus();
            $("input#email").val('Vendosni nje e-mail te sakte');
         return false;
        }
    });

    /*form submit*/
    $("form#form-email").submit(function() {
        var email = $("input#email").val();
        $.ajax({
            url:'mail.php',
            type:'post',
            data: "email="+email,
            success: function(msg){
                if (msg==1)
                    $("input#email").val('Ju faleminderit! Deshironi te regjistroni nje e-mail tjeter ?');
                else
                    $("input#email").val('Gabim! Ndodhi nje gabim ne dergim!');
            }
        });
        return false;
    });
/*end formsubmit*/
});

<form action="mail.php" method="post" id="form-email">
                    <p>
                        <input type="text" name="email" id="email" value="Vendosni e-mail tuaj qe tju njoftojme per hapjen e faqes"/>
                    </p>
                    <p>
                        <input type="submit" name="submit" id="submit" value="" />
                    </p>
                </form>

Hope that you can help me on this.

+1  A: 

very basic mail.php pointing to file called 'email.txt'

<?php

$file = fopen('email.txt', 'a');
fwrite($file, $_POST['email'] . "\n");
fclose($file);

?>
Crayon Violent
This is the "essentials".I would suggest validating the email address with server-side regex instead though. Also this script should do things like make sure the file exists, is writable, etc.. and provide a response to your ajax request if you want/need it.
Crayon Violent
does it care for file locking and makes other requests to wait till first request closes?
Ankit Jain
No. All it does is open a file, write to it, and close it. I was just listing the essentials so as to understand the working principle. There are a lot of bells and whistles to be added, yes. This is a "Here is what you NEED..." post.
Crayon Violent