SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Dec 2006
    0 Post(s)
    0 Thread(s)

    Question how to turn this PHP form into AJAX?

    Hi I was wondering how I can turn a PHP form I am using into an AJAX form so the page doesn't refresh when the submit button has been pressed? Right now the form successfully posts data into a MySql db and once submitted a valid email it says thank you!

    Below is the code I am using, if someone can help me make it work without refresh but keep all the validation and thank you message would be great!
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/jquery.1.2.6.js"></script>
    $(document).ready(function() {
    /* keep functions at the top of the page and together - keeps it tidy 
       NOTE: merged the two functions together */
    function clean_data($string) {
        if (get_magic_quotes_gpc()) {
            $string = stripslashes($string);
            $string = htmlentities($string);
        $headers = array(
        $string = preg_replace($headers, '', $string);
        return mysql_real_escape_string($string);
    /* Open database connection */
    $conn = mysql_connect('localhost', 'root', 'password');
    /* empty error message that will be filled later on */
    $errorMsg = '';
    /* if the form has been sent....
        ** only process if the hidden field 'formSend' is present in the POST Array 
        ** Using hidden fields means the user can hit enter or click the submit button */
    if(isset($_POST['formSend'])) {
    /* assign simple variables to the POST array add in you data cleansing functions here */
    $email = clean_data($_POST['email']);
     /* start doing checks for data authenticity and values */
        /* check the email is valid */
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $errorMsg .= '<span>Please enter a Valid email address</span>';
        /* if the $errorMsg variable is still empty then carry on with the processing */
        if($errorMsg == '') {
        /* start constructing the email message */
        $to = '';
        $subject = "New message: $topic";
        $message = "$name said: $comments";
        $headers = 'From: '. $email . "\r\n" .
        'Reply-To:' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $message, $headers);
    if(mail($to,$subject,$message, $headers))
    { echo ("<span id='thankyou'>Thank you</span>"); }    
        /* Insert data into the database table after it has been cleaned */
    $query = " INSERT INTO submissions (email) VALUES
    		mysql_query($query) or die(mysql_error());
        // Close connection
        } else {
            /* could do something else here
                ** */
    } /* end if isset SUBMIT */
    /* if the errorMsg variable isn't empty  echo it */
    if($errorMsg != '') { echo $errorMsg; } 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><fieldset><legend></legend>
    <table border="0" cellspacing="0" cellpadding="0" style="margin:0 0 0 50px;">
        <th><label for="email">Email address:</label></th>
        <td><input type="text" id="email" name="email" value="<?php echo $_POST['email']; ?>" /></td>
        <td><?php echo($errorMsg); ?> </td>
        <td align="right">
        <input type="hidden" name="formSend" value="1" />
      <input type="submit" name="formSend" value="submit" /> </td>
    </table> </fieldset>
    Last edited by shtoom; Apr 30, 2009 at 09:19.

  2. #2
    SitePoint Addict ChiefLee's Avatar
    Join Date
    Apr 2009
    Lawrence KS
    0 Post(s)
    0 Thread(s)
    Since you're using jQuery, you might have a look at the jQuery form plugin:

    You'll need to refactor your code to separate display logic from form validation, so that you can return a proper XML or JSON response if posting via AJAX.
    Add your logo to Pocket Folders.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts