0

I hope that you can help me with a problem that I have. I have a contact form, and I am getting an error log like this:

PHP Notice: Undefined index: Name in {path}/contactengine.php on line 6
PHP Notice: Undefined index: Email in {path}/contactengine.php on line 7
PHP Notice: Undefined index: Message in {path}/contactengine.php on line 8

my PHP code and HTML are:

<?php

$EmailFrom = "";
$EmailTo = "admin@memwdesings.com";
$Subject = "From website";
$Name = Trim(stripslashes($_POST['Name']));      // line 6
$Email = Trim(stripslashes($_POST['Email']));    // line 7
$Message = Trim(stripslashes($_POST['Message']));// line 8

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://arquitectura-om.com/\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
<!-- Contact Form -->
<div class="col-md-5">
  <div class="contact-header">Send us a Message</div>
  <form method="post" action="contact/contactengine.php">
    <div class="control-group form-group">
      <div class="controls">
        <label>Name:</label>
        <input type="text" class="form-control" id="Name" required data-validation-required-message="Please enter your name.">
      </div>
    </div>
    <div class="control-group form-group">
      <div class="controls">
        <label>Email:</label>
        <input type="email" class="form-control" id="Email" required data-validation-required-message="Please enter your email address.">
      </div>
    </div>
    <div class="control-group form-group">
      <div class="controls">
        <label>Message:</label>
        <textarea rows="10" cols="100" class="form-control" id="Message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
      </div>
    </div>
    <div id="success"></div>
    <!-- For success/fail messages -->
    <button type="submit" class="contact-btn btn">Send Message</button>
  </form>
</div>
MyStream
  • 2,533
  • 1
  • 16
  • 33
  • Welcome to SO. Please take a [tour](https://stackoverflow.com/tour) and read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). To get a better answers on topics you should add a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and read [How to ask](https://stackoverflow.com/help/how-to-ask) to your question. – buhtz May 13 '18 at 10:31
  • You need to assign "name"s to your input elements. ID's aren't the same... – arkascha May 13 '18 at 10:32
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Nico Haase May 13 '18 at 10:34
  • What micmackusa means is if you assign $_POST[’Name’] to a variable the matching input field need the name attribute set to the POST index. otherwise your POST array won’t have that value. If your processing code is running on the same file like your form html you might wrap that PHP code with isset condition because the POST array will only exist after submitting the form. – Luckyfella May 13 '18 at 11:45

3 Answers3

1

No name attributes in your form fields means no values submitted.

id attributes don't count.

Basically, if you don't need the id attribute for css, js, or the labels, just replace id with name.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

Give your submit button a name submit and put all php code inside a if condition in contactengine.php file like

if(isset($_POST['submit'])){
print_r($_POST);
}
Pavan Sikarwar
  • 833
  • 5
  • 14
  • 1
    This is sometimes helpful, but it is _not_ an answer to the issue asked in the question. – arkascha May 13 '18 at 10:33
  • I think this is a reasonable suggestion. Don’t get why it was downvoted – Luckyfella May 13 '18 at 10:34
  • @arkascha the script tries to set a variable based on a POST value that does not exist unless you did send the form. That’s why it is correct to check if the form was submitted – Luckyfella May 13 '18 at 10:37
  • But you are right in the way that it is not the only issue - of course you need name attributes on your input fields as well – Luckyfella May 13 '18 at 10:38
  • User is accessing form data without submitting form thats why it is throwing an errors and also form inputs not have names to contain values – Pavan Sikarwar May 13 '18 at 10:41
  • 1
    @Luckyfella As said: it does make sense, but this is not an answer to the question. It makes little sense to add everything that might make sense as an "answer". This should have been a comment maybe, sure, but _not_ an answer. The specific issue the OP here faces is that his input elements do not have a name. That is not fixed by the snippet suggested here. So it is not an answer. – arkascha May 13 '18 at 10:41
  • I did not suggested inputs name because i don't know which name they want and also i dont suggested them to access value inside if condition with any name, just printed all result – Pavan Sikarwar May 13 '18 at 10:44
  • Agree wirh you. Both answers would have to be combined because you are going to get an error even after adding the name attribute when the page is called without submission. – Luckyfella May 13 '18 at 10:45
  • I don't know why ? – Pavan Sikarwar May 13 '18 at 10:49
  • Now I'm more confused :S I have to contain all the PHP with an if condition and give the btn submit name submit. now what else I have to do? Sorry for my ignorance I'm really new with all this PHP – Meir De La Vega May 13 '18 at 10:52
  • 1
    Give names form inputs too what ur accessing from $_POST in your php script. – Pavan Sikarwar May 13 '18 at 10:59
-1

Hi you have not given the name attribute of form elements submitted.

You have to give name of each form element and then use that name to get values in submitted action of form like in $_POST['name']