I have written some PHP code for a contact form for my one page portfolio, when it is submitted it just opens up a blank page and also the e-mail isnt sent and the text is not echo'd out. I am not very confident with PHP as it is fairly new to me. if anyone could help that would be great?
html -
<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
<li>
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
</li>
<li>
<label for="email">Email</label>
<input id="email" name="email" type="email" placeholder="example#domain.com" required>
</li>
<li>
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text" placeholder="What the message is about" required>
</li>
<li>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
</textarea>
</li>
<li>
<label for="human">What is 2 + 2 ?</label>
<input id="human" name="human" type="number" placeholder="Please insert answer" required>
</li>
</ol>
</fieldset>
<fieldset>
<input class="button" id="submit" type="submit" value="Send it!">
</fieldset>
</form>
php -
<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: $email';
$to = 'ben_humphries#hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$human = $POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) { //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
} else {
echo "<p>Something went wrong, go back and try again!</p>";
}
} else if ($_POST['submit'] && $human != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>
There were a few things wrong with your form and handler.
In your HTML form, you did not have an input for human so I added that, plus your submit button was not named, so that alone would not have worked.
<input class="button" id="submit" name="submit" type="submit" value="Send it!">
And in your PHP handler, your (mail headers) were improperly formatted, so that ended up in my SPAM box so I added that as well.
$from = $_POST['email'];
as well as headers plus I fixed your last condition to:
if (!isset($_POST['submit']) && ($_POST['human']) != '4')
HTML form
<form id="cont-form" method="post" action="mail.php">
<fieldset>
<legend>Send me a message</legend>
<ol>
<li>
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="First and last name" required autofocus>
</li>
<li>
<label for="email">Email</label>
<input id="email" name="email" type="email" placeholder="example#domain.com" required>
</li>
<li>
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="Eg. 07500000000" required>
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text" placeholder="What the message is about" required>
</li>
<li>
<label for="human">What is 2 + 2 ?</label>
<input id="human" name="human" type="number" placeholder="Please insert answer" required>
</li>
<li>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Insert your message or question here" rows="10" cols="50">
</textarea>
</li>
</ol>
</fieldset>
<fieldset>
<input class="button" id="submit" name="submit" type="submit" value="Send it!">
</fieldset>
</form>
PHP handler, tested and working for you.
<?php
$name = $_POST['name']; //'name' has to be the same as the name value on the form input element
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = $_POST['email'];
$to = 'ben_humphries#hotmail.co.uk'; //set to the default email address
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && ($_POST['human']) == '4') {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
if (!isset($_POST['submit']) && ($_POST['human']) != '4') {
echo "<p>You answered the anti-spam question incorrectly!</p>";
}
?>
Related
I am new to PHP but have a site that I need to go live and I cant get the form to work.
Basically what it does is send JUST whats in the "message" field. All other fields are blank for some reason. Also, if possible, I would like help getting it to put a confirmation of "sent mail" without redirecting to a new page, but rather just a pop up message in the form itself.
So here is the code.
<form method="post" action="index.php">
<div class="form-group">
<input class="form-control" name="name" placeholder="Name" type="text">
</div>
<div class="form-group">
<input class="form-control" name="email" placeholder="Email" type="text">
</div>
<div class="form-group">
<input class="form-control" name="text" placeholder="Which class are you interested in?" type="text">
</div>
<div class="form-group">
<input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
</div>
<input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>
and here is the PHP
<?php
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
$body = "From: $Name\n E-Mail: $Email\n Class: $Class\n Phone: $Phone\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>';
}
}
?>
Any help here would be greatly appreciated.
Made one change to this code,
<html>
<form method="post" action="mail.php">
<div class="form-group">
<input class="form-control" name="name" placeholder="Name" type="text">
</div>
<div class="form-group">
<input class="form-control" name="email" placeholder="Email" type="text">
</div>
<div class="form-group">
<input class="form-control" name="class" placeholder="Which class are you interested in?" type="text">
</div>
<div class="form-group">
<input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
</div>
<input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>
</html>
And check the PHP file,
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
var_dump($_POST);
?>
All the variables are being passed.
For having the answer in the same page without redirecting, use jQuery AJAX requests like this example. Use Javascript. http://www.formget.com/submit-form-using-ajax-php-and-jquery/
I'll post the answer for your solution if you still have issues with the AJAX.
Assign the $_POST values inside the if ($_POST['submit']) check.
<?php
if ($_POST['submit']) {
$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['text'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site';
$to = '**********#gmail.com';
$subject = 'Message from **** Site';
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>';
}
}
?>
For submit the form without page refresh use jQuery.post().
http://api.jquery.com/jquery.post/
HTML:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
</li>
<li>
<label for="name">Email*</label>
<input type="email" name="email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="name">Website</label>
<input type="url" name="website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea rows="6" cols="40" name="message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
HTML
/* Subject and Email Variables */
$emailSubject = 'Contact Form';
$webMaster = 'email#address.com';
/* Gathering Data Variables */
$nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message'];
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered as HTML */
There are more problems, so:
you missed name attribute at the name text input
you used incorect names, PHP is case-sensitive in keys ($_POST['name'] !== $_POST['Name']). This is the point why you received blank meesages
for attribute in labels has to correspond with input ID
Updated code:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="Name">Name*</label>
<input type="text" id="Name" name="Name" placeholder="Your Name" required />
</li>
<li>
<label for="Email">Email*</label>
<input type="email" name="Email" id="Email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name#something.com"</span>
</li>
<li>
<label for="Website">Website</label>
<input type="url" id="Website" name="Website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="Message">Message:</label>
<textarea rows="6" id="Message" cols="40" name="Message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
I think you got typo there, change
$headers = "From: $email\r\n";
to
$headers = "From: $emailField\r\n";
Remember to validate the $emailField to avoid injections!
Update: Now that I can see the actual HTML for the form, there is some problems.
I think for requires and ID field, so you should add one to every field in the form.
You also forgot to set name in the name text field.
Lets add the name attribute in the name field, and add the ID field (which should be done for the other fields also):
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
to
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your Name" required />
$_POST['Name'] is case-sensetive, so you should change your code so the variables match the names in the html.
example:
$nameField = $_POST['Name'];
should be
$nameField = $_POST['name'];
put $nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message']; within a post function. After verifying $_post is not empty. on form submit.
This question already has answers here:
How to show or hide a form using php?
(5 answers)
Closed 9 years ago.
I have a contact form that I would like to hide after someone has completed and submitted their information. Below is the code. Ideally, I would like to hide everything between the HTML fieldset tags. I know this can be done with jQuery, but is it possible with PHP?
<form id="contact" method="post" action="index.php">
<fieldset>
<label for="name">Full Name<span class="red"> *</span></label>
<input type="text" name="name" title="Enter your name" required>
<label for="email">E-mail Address<span class="red"> *</span></label>
<input type="email" name="email" placeholder="yourname#domain.com" title="Enter your e-mail address" required>
<label for="phone">Phone Number</label>
<input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555">
<label for="message">Questions and Comments<span class="red"> *</span></label>
<textarea type="text" name="message" title="Enter your questions or comments" required></textarea>
<label>What is 4+2? (Anti-spam)<span class="red"> *</span></label>
<input type="text" name="human" placeholder="Answer Here" title="Answer Here" required>
<input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" />
<p class="red">
* Indicates the field is required.
</p>
</fieldset>
<!-- Contact Form Details -->
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'This message has been sent from your website.';
$to = 'user#domain.com';
$subject = 'From your contact form';
$human = $_POST['human'];
$body = " From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '6') {
if (mail($to, $subject, $body, $from)) {
echo '
<h2>Thank You!</h2>
<span class="message">
Your message has been sent!
</span>';
} else {
echo '
<h2>Oops!</h2>
<span class="message">
Something went wrong, go back and try again!
</span>';
}
} else if ($_POST['submit'] && $human != '6') {
echo '
<h2>Incorrect Answer!</h2>
<span class="message">
The correct answer is 6. Please try again.
</span>';
}
?>
</form>
UPDATE: After trying a few suggestions, I realized that since the page was reloading after the form was submitted, trying to hide the form was pointless. You hit submit, form disappears, page reloads, form is there again. So I surrounded the from in a container and modified the CSS, so the message appears above the form, which is fine. Thank you all for the help though. It's appreciated!
On success, set a variable:
$success = true;
Then surrounding the HTML form:
<?php echo $success ? '<!--' : ''; ?>
// form here...
<?php echo $success ? '-->' : ''; ?>
Try this:
if(!isset($_POST['submit']))
{
<form id="contact" method="post" action="index.php">
<!--Contents in between-->
</form>
}
Also change your html to:
<form id="contact" method="post" action="index.php">
<fieldset>
<label for="name">Full Name<span class="red"> *</span></label>
<input type="text" name="name" title="Enter your name" required>
<label for="email">E-mail Address<span class="red"> *</span></label>
<input type="email" name="email" placeholder="yourname#domain.com" title="Enter your e-mail address" required>
<label for="phone">Phone Number</label>
<input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555">
<label for="message">Questions and Comments<span class="red"> *</span></label>
<textarea type="text" name="message" title="Enter your questions or comments" required></textarea>
<label>What is 4+2? (Anti-spam)<span class="red"> *</span></label>
<input type="text" name="human" placeholder="Answer Here" title="Answer Here" required>
<input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" />
<p class="red">
* Indicates the field is required.
</p>
</fieldset>
</form>
<!-- Contact Form Details -->
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'This message has been sent from your website.';
$to = 'user#domain.com';
$subject = 'From your contact form';
$human = $_POST['human'];
$body = " From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '6') {
if (mail($to, $subject, $body, $from)) {
echo '
<h2>Thank You!</h2>
<span class="message">
Your message has been sent!
</span>';
} else {
echo '
<h2>Oops!</h2>
<span class="message">
Something went wrong, go back and try again!
</span>';
}
} else if ($_POST['submit'] && $human != '6') {
echo '
<h2>Incorrect Answer!</h2>
<span class="message">
The correct answer is 6. Please try again.
</span>';
}
?>
Dont include Contact Form Details inside <form>...</form>
Why not put the php code above the form and add an if condition? If you want to show the form only one time for the whole browser session, you have to use php sessions to store an flag.
<form id="contact" method="post" action="index.php">
<fieldset>
<!-- Contact Form Details -->
<?php
$success = false;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'This message has been sent from your website.';
$to = 'user#domain.com';
$subject = 'From your contact form';
$human = $_POST['human'];
$body = " From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '6') {
if (mail($to, $subject, $body, $from)) {
$success = true;
echo '
<h2>Thank You!</h2>
<span class="message">
Your message has been sent!
</span>';
} else {
echo '
<h2>Oops!</h2>
<span class="message">
Something went wrong, go back and try again!
</span>';
}
} else if ($_POST['submit'] && $human != '6') {
echo '
<h2>Incorrect Answer!</h2>
<span class="message">
The correct answer is 6. Please try again.
</span>';
}
if ($success) {
?>
<label for="name">Full Name<span class="red"> *</span></label>
<input type="text" name="name" title="Enter your name" required>
<label for="email">E-mail Address<span class="red"> *</span></label>
<input type="email" name="email" placeholder="yourname#domain.com" title="Enter your e-mail address" required>
<label for="phone">Phone Number</label>
<input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555">
<label for="message">Questions and Comments<span class="red"> *</span></label>
<textarea type="text" name="message" title="Enter your questions or comments" required></textarea>
<label>What is 4+2? (Anti-spam)<span class="red"> *</span></label>
<input type="text" name="human" placeholder="Answer Here" title="Answer Here" required>
<input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" />
<p class="red">
* Indicates the field is required.
</p>
<?php } ?>
</fieldset>
</form>
i'm getting blank field returns in my inbox on only a portion on my form fields
like so:
From: whatevername
Email: fgsdfg#fakeysite.com
Message:
phone:
here is my code its probably something really dumb but its bugging me, so here we go.
HTML
<div class="row-item col-1_4">
<h3>Contact Form</h3>
<h4>Please fill out the form to get your free CD Replacement Kit</h4>
<!-- Success Message -->
<div class="form-message"></div>
<!-- Form -->
<form class="b-form b-contact-form" action="blast.php">
<div class="input-wrap m-full-width">
<i class="icon-user"></i>
Name
<input class="field-name" type="text" placeholder="Name (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-phone"></i>
Phone
<input class="field-phone" type="text" placeholder="Phone">
</div>
<div class="input-wrap m-full-width">
<i class="icon-envelope"></i>
Email
<input class="field-email" type="text" placeholder="E-mail (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-pencil"></i>
Message
<input class="field-comments" type="text" placeholder="Message">
</div>
<input class="btn-submit btn colored" type="submit" value="Send">
</form>
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient = "csmith#legacybrokerage.com";
$subject = "UNLIMITED ANNUITY LEADS CD BLAST";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
try this, define name= 'something' to the input field
<input class="field-name" type="text" placeholder="Name (required)" name="name">
<input class="field-phone" type="text" placeholder="Phone" name="phone">
<input class="field-email" type="text" placeholder="E-mail (required)" name="email">
your input's dose not name attribute!
<input class="field-name" type="text" placeholder="Name (required)">
correct :
<input name="from" class="field-name" type="text" placeholder="Name (required)">
I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.