When the submit button is clicked, the browser goes to www.****.com/contact.php and the page is blank. The email is also not delivered. This is my first time dealing with php. What am I missing?
Here is the form:
<form class="comment-form" action="contact.php" method="POST">
<p class="comment-notes">Your email address will not be published. All fields are required.</p>
<p class="comment-form-email">
<label for="author">Name</label>
<span class="required">*</span>
<input id="author" type="text" class="input-text" name="name">
</p>
<p class="comment-form-author">
<label for="email">Email</label>
<span class="required">*</span>
<input id="email" type="text" class="input-text" name="email">
</p>
<p class="comment-form-url">
<label for="subject">Subject</label>
<span class="required">*</span>
<input id="subject" type="text" class="input-text" name="subject">
</p>
<p class="comment-form-comment">
<label for="message">Message</label>
<textarea name="message" id="message" cols="45" rows="10" class="input-text"></textarea>
</p>
<p class="form-submit">
<input class="btn btn-md btn-default" name="submit" type="submit" id="button" value="Send"><input type="reset" value="Clear">
</p>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$formcontent = "From: $name\n Message: $message";
$recipient = "me#example.com";
$subject = "$subject";
$mailheader = "From: $email \r\n";
error_reporting(E_ALL);
ini_set(display_errors, 1);
mail($recipient, $subject, $formcontent, $mailheader) or die ("Error!");
echo "Thank You! We will respond to your inquiry as soon as possible"; " -"<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>;
?>
It looks like this form is posting to itself? IF that's the case, I think you should probably be using
<form class="comment-form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
htmlspecialchars() is a safety thing. Helps to prevent against some hacks.
Looks like your PHP isn't being called properly because it isn't being triggered by the submit button. The code below executes when the submit button is pressed.
if(isset($_POST['submit']
{
// put your PHP code here, this executes when submit is...submitted
}
Give this a shot, it should help you out some.
<?php
error_reporting(E_ALL);
ini_set(display_errors, 1);
if(isset($_POST['submit']))
{
//Gather the POST info and set them to variables
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Setup the message and define who will be emailed the info
$formcontent = "From: $name\n Message: $message";
$recipient = "pbentley07#gmail.com";
$headers = 'From:' . $email;
mail("$recipient", $subject, $formcontent, $headers) or die ("Error!");
echo "Thank You! We will respond to your inquiry as soon as possible! - <a href='contact.html' style='text-decoration:none;color:#ff0099;'>Return Home</a>";
}
?>
<form class="comment-form" action="contact.php" method="POST">
<p class="comment-notes">Your email address will not be published. All fields are required.</p>
<p class="comment-form-email">
<label for="author">Name</label>
<span class="required">*</span>
<input id="author" type="text" class="input-text" name="name">
</p>
<p class="comment-form-author">
<label for="email">Email</label>
<span class="required">*</span>
<input id="email" type="text" class="input-text" name="email">
</p>
<p class="comment-form-url">
<label for="subject">Subject</label>
<span class="required">*</span>
<input id="subject" type="text" class="input-text" name="subject">
</p>
<p class="comment-form-comment">
<label for="message">Message</label>
<textarea name="message" id="message" cols="45" rows="10" class="input-text"></textarea>
</p>
<p class="form-submit">
<input class="btn btn-md btn-default" name="submit" type="submit" id="button" value="Send"><input type="reset" value="Clear">
</p>
</form>
in your echo statement, you have bad syntax. Try
echo "Thank You! We will respond to your inquiry as soon as possible - <a href='contact.html' style='text-decoration:none;color:#ff0099;'>\"Return Home\"</a>";
Could you confirm that the page+code you posted here
is contact.php file?
Try to fix your php code first, this line:
echo "Thank You! We will respond to your inquiry as soon as possible"; " -"<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>;
must be:
echo "Thank You! We will respond to your inquiry as soon as possible";
?>
Return Home
<?php
You have some serious "errors" in your PHP:
$message is not defined
$subject is not defined
It's ini_set('display_errors', 1);, not ini_set(display_errors, 1);
Your echo line is invalid, remove the additional HTML part
Your final code should looke like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $name\n Message: $message";
$recipient = "me#example.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die ("Error!");
echo "Thank You! We will respond to your inquiry as soon as possible";
?>
<a href='contact.html' style='text-decoration:none;color:#ff0099;'> "Return Home"</a>
You might also want to read up on form validation
Related
The HTML code is this
<form class="rnd5" action="mail.php" method="post">
<div class="form-input clear">
<label for="name">Nume <span class="required">*</span><br>
<input type="text" name="name" id="name" value="" size="22">
</label>
<label for="email">Email <span class="required">*</span><br>
<input type="text" name="email" id="email" value="" size="22">
</label>
</div>
<div class="form-message">
<textarea name="message" id="message" cols="25" rows="10"></textarea>
</div>
<p>
<input type="submit" value="Submit" class="button small orange">
<input type="reset" value="Reset" class="button small grey">
</p>
</form>
And the php code is this one :
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "pascal.m.cornel#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
But it doesnt work.... i don't know what to do , i need some help here please.
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.
Source: http://php.net/manual/en/function.mail.php
In your case, neither \r\n nor \n is needed as you're only using a single extra header.
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.
Hi guys i'm having a issue i hope you guys can help with, i'm typing in all the fields and then upon pressing submit i'm getting just "Error!" on my screen.
Please see the code:
HTML
<h2 class="formhead">Contact Form</h2>
<br>
<form class="form" action="mail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<br>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
<label for="email">Email</label>
</p>
<br>
<p class="number">
<input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
<label for="name">Contact Number</label>
</p>
<br>
<p class="web">
<input type="text" name="web" id="web" placeholder="www.example.co.uk" />
<label for="name">Website</label>
</p>
<br>
<p class="message">
<textarea name="message" id="message" placeholder="Write something to us" /> </textarea>
</p>
<br>
<p class="submit">
<input type="submit" value="Send"/>
</p>
</form>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$website = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Any help would be much appreciated!
Thanks
Sam
Your script always reporting 'Error!' because the mail() function always fails. That's because some index you're using in the php file doesn't match to the input names in your form:
Change these:
$website = $_POST['website'];
to:
$website = $_POST['web'];
Or change it in your form.
Also you have to specify a name for the message textarea:
<textarea name="message" id="message" placeholder="Write something to us" />
This may fail again if it can't connect to mailserver. This is probably you're case if The SMTP is Disabled.
As per my comment, here's an example of a better die statement:
<?
$your_function or die("Error! a") // Just replace the letter a with anything. It serves as a simple link to your function that only you know. so you can go back and check it
i have a form that work with jquery this form is :
download link for this is
http://www.4shared.com/rar/qkIP2Ulb/magical-contact-form.html
<form class="fcf-contact-form" action="mail.php" method="post">
<div class="right">
<input type="text" name="name" id="name" class="input">
<input type="text" name="email" id="email" class="input">
<div class="left">
<textarea name="message" cols="0" rows="5" class="textarea"></textarea>
<input type="submit" name="submit" value="Send message !" class="submit"/>
</div>
</div>
</form>
and mail.php file code is that code :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n email: $email \n message: $message";
$recipient = "milad.esmailzade#yahoo.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<a href='your page link here.php' style='text-decoration:none;color:#ff0099;'></a>";
?>
i upload this contact form on the host and when i click send i dont recieve email from this form !
what is the problem ?
You have to ask your hosting technical support why the PHP mail function does not work?
Try setting SMTP and user parameters, like this:
ini_set("SMTP","mail.mydomain.com" ); // or the IP
ini_set('sendmail_from', 'Me#mydomain.com'); // Account used to deliver the messages.
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>';
}}
?>