1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with contact form

Discussion in 'HTML & Website Design' started by igloodesigns, Jan 8, 2013.

  1. #1
    I'm using a simple form in HTML like so,

    
                <form method="post" action="mail.php">
                    <label>Name</label>
                    <input name="name" placeholder="Type Here">
                    <label>Email</label>
                    <input name="email" type="email" placeholder="Type Here">
                    <label>Message</label>
                    <textarea name="message" placeholder="Type Here"></textarea>
                    <label>*What is 2+2? (Anti-spam)</label>
                    <input name="human" placeholder="Type Here">
                    <input id="submit" name="submit" type="submit" value="Submit">
    </form>
    
    HTML:
    And here's the PHP code for mail.php.

    
    <?php
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'From: TangledDemo'; 
        $to = 'metalwolf92@gmail.com'; 
        $subject = 'Hello';
        $human = $_POST['human'];
                
        $body = "From: $name\n E-Mail: $email\n Message:\n $message";
                    
        if ($_POST['submit']) {
            if ($name != '' && $email != '') {
                if ($human == '4') {                 
                    if (mail ($to, $subject, $body, $from)) { 
                    echo '<p>Your message has been sent!</p>';
                } else { 
                    echo '<p>Something went wrong, go back and try again!</p>'; 
                } 
            } else if ($_POST['submit'] && $human != '4') {
                echo '<p>You answered the anti-spam question incorrectly!</p>';
            }
            } else {
                echo '<p>You need to fill in all required fields!!</p>';
            }
        }
    ?>
    
    PHP:
    How can I echo the message "Your message has been sent!" on the same page of the contact form, instead of showing the results on the mail.php page? +Rep to anyone who helps.
     
    igloodesigns, Jan 8, 2013 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    First - write all that checking code in Javascript. Don't send the data to the PHP file unless the required fields are filled in and the anti-spam question was answered correctly.

    Then, don't submit the form, send the data to the PHP page using AJAX (there are tutorials all over the web). When you get the return from mail() (in the PHP file), echo back whether it worked or failed, and the page (you're still on the form page) can tell the user that the email was sent or that there was a problem.
     
    Rukbat, Jan 9, 2013 IP