youngdero youngdero - 3 months ago 14
PHP Question

Notice: Undefined index: error

I am trying to send email via a web form, but I get this undefined index error

Undefined index: email in C.....
Undefined index: phone in C....,
Undefined index: name in C......
Undefined index: message in C.....


below is my php code

<?php
session_start();

//error_reporting(0);

if(isset($_POST['submitted'])) {
{
$sendto = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$phone = $_POST['phone'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $sendto;

$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$from."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$message."</p>\r\n";
$msg .= "<p><strong>Name:</strong> ".$name."</p>\r\n";
$msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n";
$msg .= "</body></html>";


if(mail($sendto, $subject, $msg, $headers)) {

echo '<center><p style="color:green">Mail Sent. Thank you, we will contact you shortly.</p></center>';

} else {

echo '<center><p style="color:red">Mail not Sent. Try Again.</p></center>';
}
}

}
?>


but when I submit the form it throws those errors
and below is my form that

<form name="submitted"action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="text" id="name" required />
<label>Name</label>
<span></span>
</div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="email" id="email"required />
<label>Email</label>
<span></span>
</div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="tel" id="phone"required />
<label>Phone</label>
<span></span>
</div>
<div class="styled-input wide wow slideInUp animated animated" data-wow-delay=".5s">
<textarea id="message" required></textarea>
<label>Message</label>
<span></span>
</div>
<div class="send wow shake animated animated" data-wow-delay=".5s">
<input name="submitted" type="submit" value="Send" >
</div>
</form>


How can I fix this error?
Please any help will be well appreciated

Answer

Change this:

<input type="text" id="name" required />

To this:

<input type="text" id="name" name="name" required />

Repeat for all the <input> tags (and the <textarea>) that do not have name attributes. name attributes are required in order for the form data to be sent and be made available in $_POST.

Comments