-3

I am trying to create a contact form that sends an email to my email account(Google). I found a nice tutorial, copied the code and replaced some of it with what I need (Used a tutorial because I am completely green to PHP and this area.) I need a form that sends me an email from my website but does not redirect to a different page when I click the submit button. I only want it to say "Success" or "An error occurred."

My problem is when in XAMPP I click the submit button and get the following errors: Notice: Undefined index: name in C:\xampp\htdocs\THV\process.php on line 2

Notice: Undefined index: email in C:\xampp\htdocs\THV\process.php on line 3

Notice: Undefined index: message in C:\xampp\htdocs\THV\process.php on line 4 Your message has been sent!

So even though it says message is sent I never got a email in my email.

My question is How can I create a form that sends me an email of the input of the form boxes AND get this php to not change the page but display "your message has been sent" below the submit button? Here is my code:

<?php $name=$_POST['name'];
 $email=$_POST['email'];
 $message=$_POST['message'];
 $from='From: Test';
 $to='versionabstracts@gmail.com';
 $subject='Test';
 $body="From: $name\n E-Mail: $email\n Message:\n $message";
 if ($_POST['submit']) {
  /* Anything that goes in here is only performed if the form is submitted */
}
if ($_POST['submit']) {
  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>';
  }
}
?>
<div class="container">
  <div class="row">
    <div class="five columns">
      <form method="post" action="process.php">
        <label>Name</label>
        <input name="" placeholder="Name">
        <label>Email</label>
        <input name="" type="email" placeholder="Email">
        <label>Message</label>
        <textarea name="" placeholder="Message"></textarea>
        <input id="submit" name="submit" type="submit" value="Submit">
      </form>
    </div>
  </div>
</div>
Micah Friesen
  • 60
  • 2
  • 15
  • 1
    the name attribute are not given that's why it's giving the error. – Alive to die - Anant May 23 '15 at 02:38
  • And also: [PHP mail form doesn't complete sending e-mail](http://stackoverflow.com/q/24644436) – mario May 23 '15 at 02:39
  • This question is not a duplicate because it is about multiple things, not just the error. I ask how to make it send the email without leaving the page, and putting the "Success" Text under the button. – Micah Friesen May 23 '15 at 02:41
  • Making your question overly broad and about multiple issues does not prevent closing. – mario May 23 '15 at 02:58
  • I'm a 13 year old trying to make a website for a web desgin studio. I don;t know what I am doing, I just started web development so have lots of questions, why close if that will just make me not be able to ask questions? Next time someone down votes me my account is banned from asking. How does that help me? – Micah Friesen May 23 '15 at 03:01
  • this is why you ask ONE question per post –  May 23 '15 at 03:31
  • SO much negativity towards me. Wow. – Micah Friesen May 23 '15 at 04:58

1 Answers1

1

The name attribute of your input fields are what is used for the index of the $_POST array in PHP.

You did not set the name attributes in your HTML code. For this reason, e.g. $_POST['name'] cannot be resolved, which causes the message.

To fix this, actually fill in the attributes:

<div class="container">
  <div class="row">
    <div class="five columns">
      <form method="post" action="process.php">
        <label>Name</label>
        <input name="name" placeholder="Name">
        <label>Email</label>
        <input name="email" type="email" placeholder="Email">
        <label>Message</label>
        <textarea name="message" placeholder="Message"></textarea>
        <input id="submit" name="submit" type="submit" value="Submit">
      </form>
    </div>
  </div>
</div>
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • How do I set them, like I said I;m green to this, I'm new to it and need you to explain everything, possibly give me a updated code? – Micah Friesen May 23 '15 at 02:43
  • I did not see that until I refreshed the page, sorry. Did you edit your answer or something? You solved my errors, but could you help with the problems with the email not sending to my email address and help me get the php to not change the page, instead show the your message has been sent below the send button? – Micah Friesen May 23 '15 at 02:50
  • What do you mean by "get the php to not change the page"? – TimoStaudinger May 23 '15 at 02:57
  • It redirects to process.php when I click submit, then prints success on that page. – Micah Friesen May 23 '15 at 02:58
  • Well, you define the page the HTML form is submitted to with the `action` attribute of the `form` element. Since this says `process.php`, it will send it there. If you remove the attribute `action`, it will submit the form to the current page. Make sure, however, that the PHP code above is also on the current page in order to be able to process the output. – TimoStaudinger May 23 '15 at 03:01
  • How do I get a success message to display then? – Micah Friesen May 23 '15 at 03:04
  • The same way as before - did you even try it yet? – TimoStaudinger May 23 '15 at 03:05
  • Yes, it did not change the page but it reloaded it but did not display success. – Micah Friesen May 23 '15 at 03:06
  • It will always reload the page when you submit a form and use only HTML and PHP. Where exactly did you put your PHP code on the page? Please update your question with the code – TimoStaudinger May 23 '15 at 03:08
  • I put the PHP in a separate document called process.php. – Micah Friesen May 23 '15 at 03:10
  • Well, it won't be executed then. How should it? – TimoStaudinger May 23 '15 at 03:12
  • I don't know! Thats why I am asking this question! So what do I do to get the code to send me an emal? The only code I have is this. The PHP and HTML shown, plus a correct html document. – Micah Friesen May 23 '15 at 03:14
  • As I said, if you put the PHP code into the HTML file that is called when the form was submitted, your code will be executed. – TimoStaudinger May 23 '15 at 03:15
  • But I don't get an email to my account. I want the code to send the inputted text in all the boxes into an email then email it to versionabstracts@gmail.com – Micah Friesen May 23 '15 at 03:16
  • This might have a thousand reasons. Where do you host the code? Does the host support sending email? Is it configured correctly? You should find some leads on how to fix it by googling your problem. – TimoStaudinger May 23 '15 at 03:18
  • The code is being run on my computer in XAMPP. I don't know how to configure things. Is all my code working? What do I but in the from email in the php? – Micah Friesen May 23 '15 at 03:20
  • If you run your PHP locally on XAMPP, you most probably won't be able to send mails without a lot of additional effort. – TimoStaudinger May 23 '15 at 03:28
  • So are you saying this will work if I buy a domain on go-daddy with their hosting? – Micah Friesen May 23 '15 at 04:57