I've got a simple email form in PHP that is to send a form input to the email address. However, the textarea isn't included. The phone number is being sent, just the textarea is not. Here's my form:
<form id="form1" name="form1" method="post" action="send.php">
<p> <span id="sprytextfield1">Your name:
<label for="name"></label>
<input type="text" name="name" id="name" /></p>
<span class="textfieldRequiredMsg">A value is required.</span></span>
<p><span id="sprytextfield2">
<label for="email">Email address:</label>
<input type="text" name="email" id="email" />
<span class="textfieldRequiredMsg">A value is required.</span></span></p>
<p>
<label for="phone">Phone Number:</label>
<input type="text" name="phone" id="phone" />
</p>
<p><span id="sprytextarea1">
<label for="message">Message</label>
<br />
<textarea name="message" id="message" cols="45" rows="5"></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></p>
<p>
<input type="submit" name="Submit" id="Submit" value="Submit" />
<br />
</p>
</form>
After that, I have the validation for the form.
Here's the php
$name=$_POST[name];
$email=$_POST[email];
$phone=$_POST[phone];
$message=$_POST[message];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("my#email.com", $subject, $from, $phone, $message);
echo "Email sent!";
change your
$name=$_POST[name];
$email=$_POST[email];
$phone=$_POST[phone];
$message=$_POST[message];
to
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
your message will work with this way
Hmmm, you aren't using parameters in the correct order. As per mail documentation (http://php.net/manual/en/function.mail.php), it should be:
mail("my#email.com", $subject, $message, $from);
I am not sure where you want to use $phone but may be you can concatenate it to $message before sending it.
This worked, for some reason. Using $subject, $message, $from would only send the message. I got rid of the phone number.
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Someone from your website is contacting you";
mail("me#email.com", $subject, $from, $message); //weird, but it works
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
Problem is when I submit this form it shows something went wrong. Could somebody check where I've made a mistake.
HTML code:
<div class="main">
<div class="info">Give your feedback</div>
<form action="mail.php" method="post" name="form" class="form-box">
<label for="name">Name</label><br>
<input type="text" name="name" class="inp" placeholder="Enter Your Name" required><br>
<label for="email">Email</label><br>
<input type="email" name="email" class="inp" placeholder="Enter Your Email" required><br>
<label for="phone">Phone</label><br>
<input type="tel" name="phone" class="inp" placeholder="Enter Your Phone" required><br>
<input type="submit" name="submit" value="Send" class="sub-btn">
</form>
</div>
PHP code
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$msg=$_POST['msg'];
$to='onlyanivar#gmail.com';
$subject='Form Submission';
$message="Name:".$name."\n"."Phone:" .$phone. "\n"."Wrote the following: "."\n\n".$msg;
if(mail($to, $subject, $message, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly</h1>";
}
else{
echo "Something went wrong!";
}
?>
You have no $headers variable declared when you call the mail function.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
CSS y PHP of my contact-form.
The page works perfectly but I'm trying to send an email and the button of send doesn't work. In the image you can see that when I try to send an email the button stays white. At the same time the button Clear works perfect! I attach my code
CONTACT.HTML
<form action="mail.php" method="POST" class="contact-form">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
MAIL.PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "sa*********#*******.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
mail function doesn't work on local server and also you need to set SMTP configuration in PHP to send mail.
Are you placing your form inside the form tag?
<form action="mail.php" method="post">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
</form>
I'm having problems with this code. After submitting the filled form the display message (Email sent!) is not displaying next to the submit button. I need to display the Email sent message next to the submit button on the contact form. After submitting the form, I also need to clear the form fields even after a refresh or a back action. Kindly help me out with this code
Form Action Images:
Before Submitting the Form
After Submitting the form
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" value="Submit">
</form>
<?php
}
else /* send the submitted data */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Email sent!";
}
}
?>
Your code should be like this. Notice isset($_POST['submit'], $msg , value="".
<?php
if(isset($_POST['submit']))
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
$msg = "All fields are required";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$msg = "Email Sent";
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" value="">
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required value="">
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" name="submit" value="Submit">
<?php echo $msg; ?>
</form>
You can redirect the page after sent email or also can set a variable like $msg='email sent'; and call this variable before your submit button.
$action=$_REQUEST['action'];
if ($action!="") /* display the contact form */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
header('location: yoururl?msg=error');
//$msg = "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('location: yoururl?msg=succss');
//$msg = 'Email Sent!';
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill the form again.";} ?>
<?php echo $msg; ?>
<input type="submit" value="Submit">
</form>
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
It sends the email, but without a body, subject or anything. This is what I get:
HTML:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" class="text" /></label>
<label for="message">Message:<textarea id="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
PHP (mail.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$headers = "From: " . $email;
mail($recipient, $subject, $message, $headers);
echo "Thanks! The message was sent. :)";
?>
Any insight? Thanks so much.
Forms fields are based on their names not their ids.
replace your :
id="(...)"
by
id="(...)" name="(...)"
You should have a look to Swift Mailer which is strongly safer than your current method.
You need to have a name attribute on your HTML input fields. That's what the form uses to create the indexes in the $_POST array. Corrected HTML is below
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
you need to give name attribute for each html element . Use below html code:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
Into my server works, are you sure that in $_POST are set?
If you can't send email assicure you can try to set SMTP to send your email
Try to use something like phpmailer to send email.
In your form for every field you have to put:
- id="example_id"
- name="example_name"