Contact form error on submit - php

I've built a basic contact form using the following:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$message = $_POST['message'];
$from = 'From: Contact';
$to = 'info#mydomain.com';
$subject = 'message';
$body = "From: $name\n E-Mail: $email\n Contact Number: $contact\n Message:\n $message";
?>
<form id="contact-1" method="post">
<label>Name</label>
<input name="name" placeholder="YOUR NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL ADDRESS">
<label>Contact Number</label>
<input name="contact" type="tel" placeholder="TELEPHONE">
<label>Message</label>
<textarea name="message" placeholder="MESSAGE"></textarea>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php
if ($_POST['submit'] ) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been submitted</p>';
} else {
echo '<p>Something went wrong, please try again!</p>';
}
}
?>
If you leave the name input blank and submit the form, its successful. However, if I add anything the name input. the form doesnt send and the site loads a 'page not found'.

Add type="text" into first input element.

This code appeared to have worked for me, test it out and see if the mail sends properly.
I added the
type="text"
to Name, and added
isset()
around
$_POST['submit']
I enclosed the top potion with
if (isset($_POST['submit']) ) {
To avoid errors before the page is first submitted.
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$message = $_POST['message'];
$from = 'From: Contact';
$to = 'info#mydomain.com';
$subject = 'message';
$body = "From: $name\n E-Mail: $email\n Contact Number: $contact\n Message:\n $message";
}
?>
<form id="contact-1" method="post">
<label>Name</label>
<input type="text" name="name" placeholder="YOUR NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL ADDRESS">
<label>Contact Number</label>
<input name="contact" type="tel" placeholder="TELEPHONE">
<label>Message</label>
<textarea name="message" placeholder="MESSAGE"></textarea>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been submitted</p>';
} else {
echo '<p>Something went wrong, please try again!</p>';
}
}
?>

Related

PHP contact form

I've looked through a lot of pages to find an answer for my problem. But I can't figure out why my form isn't working.
I tried different versions of codes from premade examples but I can't get it to work.
This is my php above my form
<?php
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'me#email.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
?>
and this is my form
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="Your Name">
<label>Email</label>
<input name="email" type="email" placeholder="Your Email">
<label>Subject</label>
<input name="subject" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
I've only changed my email in this example here the rest is the same. I'm testing this live on a modern server with php 5+ support
Basicly everything works fine except that I don't get an email.
I can't find out how to make it work sadly. Any ideas would be cool :/
Edit: GOD DAMNIT IM A TOTAL IDIOT
Gmail Spam filter is strong in this one.
You have not defined $from.
That is why it is not sending mail.
Also, please check SMTP settings for your machine/server.
SMTP ports may not be configured, that is why mail is not sending.
Try this for the form:
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" type="text" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
Also try following this working format:
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
You could just replace the value inside the " " with the post function.
Edit ( This will surely work ):
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
The fourth Parameter for the Mail function is header. Where You can set Mail form and others (http://php.net/manual/en/function.mail.php ). Please set that, and also check the SMTP configuration as "Programming Student" told you.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'to#test.com';
$from = 'from#test.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again! </p>';
}
?>
You have missed to assign the $from variable
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"> </textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
Make sure the php file name "contact.php"
Now My HTML goes this way -->
<body> Fields with * are mandatory.
<form action="mail.php" method="POST">
<fieldset>
<legend>Contact Information</legend>
<p>Name*</p> <input type="text" name="name">
<p>Email*</p> <input type="text" name="email">
</fieldset>
<br />
<br />
<fieldset>
<legend>Other Information</legend>
<p>Website</p> <input type="text" name="website">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Contact Us</option>
<option value="change">Information Change</option>
<option value="addition">Other</option>
</select>
<br />
</fieldset>
<br />
<fieldset>
<legend>Your Message</legend>
<p>Message*</p><textarea name="message" rows="8" cols="29"></textarea><br /><p>
<input type="submit" value="Send" class="but"> <input type="reset" value="Clear" class="but">
</fieldset>
</form>
</body>
and now the main thing that is PHP goes this way ->
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "youremail#domain.com";
$subject = "Contact Form";
$mailheader = "$name submited the form.";
if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
if ($email === "" || $message === "" || $name === "") {
echo "ERROR! Email and message and Name are Mandatory. <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
else {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, "Name of Form Here", "Your Form has been submitted. Your problem will be noticed soon. This is a no reply mail address. Please dont reply back. - WeBoosters India", "Thankyou") or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
}
else {
echo "The email is not valid. Please write a proper email address. - <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
?>
Hope you like it! Best of luck.

PHP comment form not parsing all fields

I know this is basic, but I am using a php comment form on my website but it is not sending me all the info inputted into the fields, please can someone help me please.
This is the HTML I'm using:
<form method="post" action="assets/php/mail.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Postcode</label>
<input name="postcode" placeholder="Type Here">
<label>Child's Date of Birth</label>
<input name="dob" placeholder="Type Here">
<label>Year your child will begin primary school in Reception class</label>
<input name="year" placeholder="Type Here">
<label>I would select York Community Free School as first choice for my child</label>
<input name="send" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<div class="form-button">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
This is the PHP I'm using:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$year = $_POST['year'];
$send = $_POST['send'];
$from = 'From: York Free School Comment form';
$to = 'yorkfreeschool#gmail.com';
$subject = 'Hello';
$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)) {
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>';
}
?>
All that is been emailed to me is the name, email and message, what am I doing wrong? Please please can the community help.
You need to add the email fields into the $body variable for it be sent I only added DOB and postcode in this example
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$year = $_POST['year'];
$send = $_POST['send'];
$from = 'From: York Free School Comment form';
$to = 'yorkfreeschool#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
//Look here
$body = "From: $name\n E-Mail: $email\n Message:\n $message Postcode: \n $postcode DOB: \n $dob \n";
if ($_POST['submit'] && $human == '4') {
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>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
You aren't using any of the additional fields you're grabbing. If you want to have them included, you need to append them to the message.
<!-- ur html code is not valid, look at submit input -->
<form method="post" action="assets/php/mail.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Postcode</label>
<input name="postcode" placeholder="Type Here">
<label>Child's Date of Birth</label>
<input name="dob" placeholder="Type Here">
<label>Year your child will begin primary school in Reception class</label>
<input name="year" placeholder="Type Here">
<label>I would select York Community Free School as first choice for my child</label>
<input name="send" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<!--</div> i think that closing div tag should go after input with id == "submit" -->
<div class="form-button">
<input id="submit" name="submit" type="submit" value="Submit">
</div> <!-- here -->
</form>

How can I send an email to an email address provided in a hidden input field?

I've got a simple PHP email form. This is going to be used for multiple people, so in order to make it easier for people, I have a hidden input field in my html form that is basically the "mailTo" email address.
How do I get the email to send the email to the address provided in the hidden input field?
Here's my HTML:
<div id="formholder">
<form method="post" action="http://www.artofliz.com/hosting/php/process.php">
<!-- THE CLIENT'S EMAIL TO ADDRESS GOES HERE!!! -->
<input name="to" type="hidden" value="lroberts#platinumstrategies.com" />
<label for="name">Name</label>
<input name="name" placeholder="Full Name" type="text" />
<label for="email">Email</label>
<input name="email" placeholder="youremail#example.com" type="email" />
<label for="phone">Phone</label>
<input name="phone" placeholder="XXX-XXX-XXXX" type="tel" />
<input id="submit" name="submit" type="submit" value="Submit" />
</form>
</div>
Here is my PHP handler:
<?php
$to = $_POST['to'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$from = 'From: noreply#platinumstrategies.com';
// $to = 'lroberts#platinumstrategies.com';
$subject = 'Free Guide Download';
$body = "From: $name\n Email: $email\n Phone: $phone";
if ($_POST['submit']) {
/* Anything that goes in here is only performed if the form is submitted */
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>';
}
}
?>
http://pastebin.com/cqZP949r - Additionally, I should note that the 'to' that's commented out works fine. It's what I'm trying to do currently that's broken.
Thank you very much for your help.

Trying to adapt a simple php contact form script

I am trying to adapt a php script to work with a contact form I am using on my site.
I get the following error when trying to view the page:
Parse error: syntax error, unexpected end of file
If I remove the script all together, I can view my page, so I think the script may be incomplete?
The form and script code is as follows:
<form name="hongkiat" id="hongkiat-form" method="post" action="index.php">
<div id="wrapping" class="clearfix">
<section id="aligned">
<input type="text" name="name" id="name" placeholder="Your name" autocomplete="off" tabindex="1" class="txtinput">
<input type="email" name="email" id="email" placeholder="Your e-mail address" autocomplete="off" tabindex="2" class="txtinput">
<input type="url" name="website" id="website" placeholder="Website URL" autocomplete="off" tabindex="3" class="txtinput">
<input type="tel" name="telephone" id="telephone" placeholder="Phone number?(optional)" tabindex="4" class="txtinput">
<textarea name="message" id="message" placeholder="Enter a cool message..." tabindex="5" class="txtblock"></textarea>
</section>
</div>
<section id="buttons">
<input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
<input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="7" value="Submit this!">
<br style="clear:both;">
</section>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$from = 'web address here';
$to = 'email here';
$subject = 'Message from mh web';
$body = "From: $name\n E-Mail: $email\n Website URL: $website\n Telehone: $telephone\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>
You need another } right before the ?>, to close the if($_POST['submit']){ block. This would be easy to see if the code were properly indented:
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>';
}
// oops! missing }
?>

Why isn't my php script working?

EDIT: Apparently I have to pay to register another domain on their site to have email capabilities. Sorry for the wasted time, and thanks for the code fixes.
I don't have any experience with php. I am just starting to figure it out. I am trying to get my form to send the entered information to my email. I set an else tag but that is all that happens. If you see any errors please let me know. I really want this to work. See for yourself: Website The codes are:
HTML:
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
<input id="send" name="send" type="hidden" value="1" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['comment'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['send'] == "1") {
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>';
}}
?>
I get Something went wrong, go back and try again! when I'm trying to submit the form.
Updated code.
<form action="post_comment.php" method="post" id="commentform">
rather than method="request"
edited to add
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
edited (in response to edited question code...) to add http://php.net/manual/en/function.mail.php
your mail() parameters are in the wrong order; should be $to, $subject, $message [, $additional_headers, etc.]
According to your code:
if ($_REQUEST['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>';
}
}
Getting the output of:
Something went wrong, go back and try again!
Means that something went wrong with the mail() function, it returned false , otherwise we would see the Your message has been sent! message.
The problem with the mail() function is that it doesn't show any errors or warning , it just returns false.
How to locate the problem?
Try writing something like:
if(mail("your.working.email#gmail.com" , "A subject for example" , "the content of this email","From: no-reply#yourdomain.com"))
echo "We are good";
else
echo "Something not workin";
If it works , check out the value of any of your posted variables right after declaring them.
echo $name;
echo $email;
echo $message;
Make sure that those variables are not empty and that the $email variable contains a legal and validated email address.
If the basic mail usage didn't work - it's something related to your php settings (php.ini) or a limitation by your server (contact your hosting company).
EDIT1: About your html form , the type attribute of the input fields should be text and not name or email.
Instead of <input type="email"...
Write <input type="text"...
<form action="post_comment.php" method="get" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>
method must be get/post
Your field "submit" is a HTML button to submit the form and will not be added to the form. Add another input field which is not visible:
<input id="send" name="send" type="hidden" value="1" />
and access the field in PHP with:
if ($_REQUEST['send'] == "1") {
I hope this helps you.
the name of the textarea is comment and you're using it as message in this line,
$message = $_REQUEST['message'];
Needs to be,
$message = $_REQUEST['comment'];
Also, the method of the form submission, needs to be POST like this,
<form action="post_comment.php" method="post" id="commentform">
please change form as follows
<form action="post_comment.php" method="post" id="commentform">
In the form you can use only get/post
Try this
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="comment" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
and in your post page..
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from \r\n";
//$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
}
-> Use POST instead of REQUEST
-> comment chaged to message
HTML
<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2" required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="message" rows="10" tabindex="4" required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email;
$to = 'powersjesse#yahoo.com';
$subject = 'WEBSITE';
$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>';
}}
?>

Categories