-1

So here is the problem I am facing. I have created a pretty simple web form:

    <form  method="post"  action="#">
                  <div  class="field"> <label  for="name">Name</label>
                    <input  name="name"  id="name"  type="text"> </div>
                  <div  class="field"> <label  for="email">Email</label> <input

                       name="email"  id="email"  type="email"> </div>
                  <div  class="field"> <label  for="message">Message</label> <textarea

 name="message"  id="message"  rows="4"></textarea> </div>
                  <ul  class="actions">
                    <li><input  value="Send Message"  type="submit"></li>
                  </ul>
                </form>

I need to know how I can use this form to send data inputed by the user to my email address [email protected] I am aware this can be done in PHP but am unsure how to approach this as I am unfamilliar with PHP. If someone could please point me in the right direction as to how this can be done, and also where I should place the PHP file relative to this, that'd be awesome!

1
  • This is also somehting you would find loads of tutorials for if you tried a site called: "Google" (google.com). Just search for "php email form" Commented Jan 17, 2016 at 13:11

1 Answer 1

0

You could copy the code below and paste it in your HTML file below your html. In your html you set the action attribute empty. Don't forget to change your file extension to .php

<?php 
if(isset($_POST['submit'])){
$to = "[email protected]";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Blablabla"; //Write whatever you want here
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page if the mail was send succesfully
} else {
header('location: contact.html'); //if it was not send succesfully it redirects to the contact page again
exit(0);
}
?>

In

header('location: contact.html');

you could also use

echo "Something went wrong. Try again later" 

or something simular. I would highly recommend you search and learn on W3Cschools or at php.net.

Sign up to request clarification or add additional context in comments.

2 Comments

That code should absolutely not go within the <body>. It tries to set HTTP headers which must be output before any of the page content.
Oh you're right thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.