Reputation: 113
I am trying to implement php for a simple form.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = '[email protected]';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
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>';
}
?>
<form class="form" method="post" action="say-hello.php">
<label>Name</label>
<input name="name" placeholder="Spongebob" required data-errormessage-value-missing="Whoa, you can't leave this blank!">
<label>Email</label>
<input name="email" type="email" placeholder="[email protected]" required data-errormessage-value-missing="Whoa, you can't leave this blank!" data-errormessage-type-mismatch="Something isn't right...">
<label>Message</label>
<textarea name="message" placeholder="Well Hello!" required data-errormessage-value-missing="Whoa, you can't leave this blank!"></textarea>
<div class="bttnholder">
<input class="submit" name="submit" type="submit" value="Submit" placeholder="Send">
</div>
</form>
I can't figure out why part of my PHP is displayed as HTML and why I get the following errors on page:
Notice: Undefined index: name in C:\xampp\htdocs\sandbox\say-hello.php on line 35
Notice: Undefined index: email in C:\xampp\htdocs\sandbox\say-hello.php on line 36
Notice: Undefined index: message in C:\xampp\htdocs\sandbox\say-hello.php on line 37
Any help to get this code working?
The following are lines 35,36,37.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
Upvotes: 0
Views: 1691
Reputation: 940
Your mail function is wrong, there is no "from" argument in it, if you want a from argument your mail function should be like this:
First define a headers variable:
$headers = "From: $from";
And your mail function:
mail($to,$subject,$body,$headers)
Upvotes: 1
Reputation: 270
The $_POST['submit'] is obsolete, as you should check for the correct form inputs. I check for every form input to be set and not to be empty. If one of them is empty or not set -> error. If all fields are given -> send the mail.
<?php
$from = 'From: Hola';
$to = '[email protected]';
$subject = 'Hola';
if(
!isset($_POST['name']) || empty($_POST['name']) ||
!isset($_POST['email']) || empty($_POST['email']) ||
!isset($_POST['message']) || empty($_POST['message'])
){
echo '<p>Please fill in all fields</p>';
}else{
$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Message:\n " . $_POST['message'] . "";
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>';
}
}
?>
Upvotes: 1
Reputation: 98
To get rid of the Notices, you should change your php to:
<?php
if(isset($_POST['name'])) {
$name = $_POST['name'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
$from = 'From: Hola';
$to = '[email protected]';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_SERVER['CONTENT_LENGTH'])) {
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>';
}
Upvotes: 1
Reputation: 1537
You must first check variable to set. Try this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = '[email protected]';
$subject = 'Hola';
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>';
}
}
Upvotes: 2
Reputation: 560
You have to insert these lines inside the if statement.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = '[email protected]';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
When the html page loads, the name for example doesn't exists. It's been set after clicking on the submit button. So if you put it inside the if statement, the script will read the variables only after the submit button is posted.
Anyway, for your future consideration, these are not errors, are warnings, the script still works.
Upvotes: 0