treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Contact Form Submit

  • Hello! i am a complete novice with PHP.. a lot of cut and pasting goes down!

    I can sort out a contact form, but they all refresh the entire screen when the 'contact.php' file is run on the submit button.

    What i want is just the form area to refresh as 'message sent'. is this possible?

    The reason i ask is because i am putting together a band site, on which there will be a 'subscribe to newsletter' section which simply collects their email address and then forwards to my email address to be storred.. this is the simplest way i could think of, but if you have other ideas, please throw them out there!

    Thanks chaps
    Alex
  • You'll want to put something like this in:
    $result = \"../contact-success.php\";
    $error = \"../contact-error.php\";

    if(mail($to, $subject, $message, $headers))
    {
    header(\"Location: \" . $result);
    }
    else
    {
    header(\"Location: \" . $error);
    }
    }
  • You want to use the jQuery Form plugin http://malsup.com/jquery/form/#getting-started

    Basically you add a script to your page as follows:
        <script type=\"text/javascript\"> 
    // wait for the DOM to be loaded
    $(document).ready(function() {
    // bind 'myForm' and provide a simple callback function
    $('#myform').ajaxForm({
    target: '#mydiv',
    });
    });
    </script>


    Where #myform is the id of your form and #mydiv is the id of the div you want to contain the message.
  • very useful! thanks guys