0

I have a contact form on my wordpress site but I cannot get information from the form to send to my email address when the submit button is clicked.

Once the submit button has been clicked and the email has been sent I would like a confirmation to appear underneath the submit button that the message has been sent/error if it hasn't. Note: I will update the css for the contact form to accommodate the extra text.

This is my form code:

<form action="secure_email.php" method="post" id="contact-form-content">
    <h5>You have had a look, so let's get cracking. Email me at me@myemail.com or use this nifty thing.</h5><br></br> 
    <legend>Contact Form</legend>
    <input type="text" placeholder="Full Name" name="full-name" id="full-name" required;><br></br> 
    <input type="text" placeholder="Email" name="email" id="email" required;><br></br>
    <textarea placeholder="Message" name="message" id="message" rows="100" cols="100" wrap="hard" required;></textarea><br></br> 
    <button type="submit" name="submit" value="submit">Send</button>
</form>

And this is secure_email.php file:

        <?php 
        if(isset($_POST['submit'])){
            $to = "me@myemail.com"; // this is your Email address
            $email = $_POST['email']; // this is the sender's Email address
            $full-name = $_POST['full-name'];
            $subject = "Form submission";
            $message = $full-name . " " . $email . " wrote the following:" . "\n\n" . $_POST['message'];

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            mail($to, $subject, $message, $headers);

 if (isset($_POST['submit'])) 
        {
        if (mail($to, $subject, $message, $headers))

        echo "Thank you for contacting me!";
        }
        else 
        { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
    ?>

Just to update all this is the working code with contact form details being sent to my given email, the confirmation message appearing within the contact form and scroll back to contact form to see the confirmation message

    <div id="contact-form">
    <form action="#contact" method="post" id="contact-form-content">
    <h5>You have had a look, so let's get cracking. Email me at me@myemail.com or use this nifty thing.</h5><br></br> 
    <legend>Contact Form</legend>
    <input type="text" placeholder="Full Name" name="fullname" id="fullname" required;><br></br> 
    <input type="text" placeholder="Email" name="email" id="email" required;><br></br>
    <textarea placeholder="Message" name="message" id="message" rows="100" cols="100" wrap="hard" required;></textarea><br></br> 
    <button type="submit" name="submit" value="submit">Send</button>
<?php 
if(isset($_POST['submit'])){
    $to = "me@myemail.com"; // this is your Email address
    $email = $_POST['email']; // this is the sender's Email address
    $fullname = $_POST['fullname'];
    $subject = "Form submission";
    $message = $fullname . " " . $email . " wrote the following:" . "\n\n" . $_POST['message'];
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

if (mail($to, $subject, $message, $headers)){
        echo "Thank you for contacting me!";
        }
        else 
        { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
?>
</form>
</div>
</div>
  • 1
    I don't see `wp_mail()` or `mail()` which sends email. – hemnath mouli Aug 11 '16 at 12:06
  • 1
    I also don't see where you send the response back to the browser so it will display the `mail sent` message to the user. – BeetleJuice Aug 11 '16 at 12:07
  • I haven't created a `mail sent` response because I don't know where to start. All the examples I have seen display the response on another html page which is not what I am looking for. I haven't used `wp_mail()` as I am not using wp email facility/plugin? I wanted to create my own form and code from scratch. Hope this makes sense? – Robertscoder Aug 11 '16 at 12:12
  • 1
    You still have no code for sending the email. How do you expect the mail to be sent if you don't actually try to send it? – John Conde Aug 11 '16 at 12:18
  • What kind of code would I be looking at? All the examples I have seen online use code for the form and then code similar to my secure_email.php. I haven't seen anything additional to this. Any pointers would be great. – Robertscoder Aug 11 '16 at 12:29

2 Answers2

0

Please use mail functions.

PHP Mail Function

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);

WordPress Mail Function

wp_mail($to, $subject, $message, $headers);
Dinesh
  • 343
  • 3
  • 11
  • I have updated my code above but still not working – Robertscoder Aug 11 '16 at 13:44
  • You can't use the hyphen(-) in PHP variable "$full-name". Please try with "$fullname" – Dinesh Aug 11 '16 at 14:10
  • I have removed the hyphens but still the same error – Robertscoder Aug 11 '16 at 14:22
  • When you click on submit button. What are you getting? I mean any issue or any message or anything else? – Dinesh Aug 11 '16 at 14:39
  • When I click submit I am directed to another page with 'Webpage cannot be found' – Robertscoder Aug 11 '16 at 14:41
  • In the WordPress you can not use the direct file path like "secure_email.php". If you want then you have to place this file on the root and in the form action you have to use "/" like "/secure_email.php". – Dinesh Aug 11 '16 at 14:44
  • Writing the full file path in the form action worked! Thank you so much! The only thing left to do is to get a confirmation message saying 'Thanks your message has been sent' above/below the submit button. At the moment I am directed to a blank page/screen once the submit button has been pressed. – Robertscoder Aug 11 '16 at 15:00
  • Sorry to update, I need to get a confirmation message above/below the submit button. At the moment I am directed to another page/screen once the submit button has been pressed. php file has been updated above. – Robertscoder Aug 11 '16 at 15:07
  • If you want to display confirmation message below the form. place all the secure file code below the form and remove the action from the form. Also I have update the code as per your requirement. – Dinesh Aug 11 '16 at 17:50
  • The update doesn't change anything. The echo message is still appearing on a new page. – Robertscoder Aug 12 '16 at 07:40
  • Please use PHP code below the form in the same file and remove the action. – Dinesh Aug 12 '16 at 08:12
  • Thank you Dinesh. I didn't read your comment properly. Sorry about that. Is there any way to stop the page scrolling back to the top once the submit button has been pressed? At the moment I have to scroll back down to the contact form to see the confirmation message. – Robertscoder Aug 12 '16 at 08:44
-1

You eitherneed to

use Ajax to post your form data to secure-email.php then add the success message on succes in JavaScript.

Or

Post to the same page as the form, detect if the form has been submitted and send your email, then echo your success message if the mail sends successfully. The page will refresh when they hit submit and the message will be displayed.

  • This doesn't really answer the question and isn't very useful in its current form. Not to mention you completely miss the fact they don't don't actually attempt to send the email in their code. – John Conde Aug 11 '16 at 12:22