I have tried to create php email form. The form was basically working, but I wanted to add validation functions like I did on 'name'
However, It doesn't work when I empty 'name' and just sent an email.
Any help would be really appreciated.
htmlfile
<form action="php_mini.php" method="post">
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email:
<input type="text" name="email">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Phone:
<input type="text" name ="phone">
<br><br>
Comment:
<!--<textarea name="comment"></textarea>-->
<!--<input type = "text" name = "comment">-->
<textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="submit" value="Submit">
</form>
php
echo '<pre>';
print_r( $_POST );
echo '</pre>';
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: Design_customers <customers.com' . " \r\n" .
//'Reply-To: vader#deathstar.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//error msg
$nameErr = $emailErr = $phoneErr = "";
$name = $email = $comment = $phone = "";
//receiver
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = $_POST['email'];
/*$message = $_POST['comment'];*/
$message = 'You got a message from a customer.:
Name: '.$_POST['name'].'
Email: '.$_POST['email'].'
Phone: '.$_POST['phone'].'
Comment: '.$_POST['comment'];
//sender
$from = 'From: Customer';
$subject = 'Customer Inquiry';
mail( $email, $subject, $message, $from );
use if(isset($_POST["name"])) to stop the error. [Notice: Undefined index: name in C........]
better you have validate this form with java Script. i have attached a snipped part of you form with javascript.
<form action="" method="post" onsubmit="return(Validate());" name="myform">
Name:
<input type="text" name="name">
<span class="error">* <div id="name_error" style="color:red;"></div> </span>
<br><br>
</form>
<script type="text/javascript">
<!--
// Form validation code will come here.
function Validate()
{
if( document.myform.name.value == "" )
{
alert( "Please provide your name!" );
name_error.textContent = "Name is Required.!";
document.myform.name.focus() ;
return false;
}
return( true );
}
//-->
</script>
Related
I'm trying to send an email using PHP mail(). I created a form that allows the user to input their information and press a submit button to send the email.
What I'm trying to do is to grab $email and set it to the $to header to send to the receiver. Everything else works perfectly, $name and $contact outputs just missing $email.
<form action="#" method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$to = $email;
$subject = "Test Email";
$message = 'Hello '.$name.', contact: '.$contact.', this is a test email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
mail($to,$subject,$message,$headers);
?>
Any tips would be appreciated
Solution:
<?php
if (!array_key_exists('Submitted',$_POST))
{
?>
<form method="post" action="#">
<input type="hidden" name="Submitted" value="true"><form action="#"
method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>
<?php
}
else
{
$name = $_POST['name'];
$contact = $_POST['contact'];
$to = $_POST['email'];
$subject = "Test Email";
$message = 'Hello '.$name.',<br /> contact #: '.$contact.', this is a test
email';
$headers = "From: Me <". strip_tags('test#mail.com') . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
}
?>
So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}
So I have some issues with my PHP Contactform on a HTML site.
Code below is what I have written in HTML. (that's not te problem)
<form action="sendmail.php" method="POST" class="contact-form">
<input type="text" placeholder="Amount" name="amount">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email Address" required name="email">
<div class="validation">
<button class="btn" name="submit">Send request</button>
// END HTML code. (BELOW is starts PHP).
$email_to = "domain#website.com";
$amount = $_POST["amount"];
$name = $_POST["name"];
$email = $_POST["email"];
$email_subject = "DOMAINNAME";
$headers = "From: " . $email . "\n";
$headers .= "Reply-To: " . $email . "\n";
$message = 'Name: ' . $name . ', email: ' . $email . ', amount: ' . $amount;
ini_set("sendmail", $email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);}
{
header("Location:https://website.com");
} else {
echo "There has been an error sending your comments. Please try later.";
}
What do I wrong? I only wanna receive the --> name, amount and mail.
Only when I press submit on my HTML site, it's send me to website.com/sendmail.php (HTTP ERROR 500).
Thank you guys.
I'm using a contact form for my website, which I validate and then email to myself, the validation is working correctly and it emails me if the user enters all the details correctly first time. However if the user enters incorrect data, then corrects it and hits send again, it won't send an email, below is the form and PHP code I have so far.
HTML code for contact form
<form action="contact.php" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
PHP code for sending the form
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
{
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
}
$send = $_POST['send'];
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "<br />";
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
if ($send)
{
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";
}
}
else
{
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
}
EDIT
In order for the code to work on the same page, you need to set the action to action=""
Otherwise, you need to use two pages. One for your form and one for contact.php which is your handler. I suggest you use two pages, but here is a version that will work inside one page.
<?php
if(isset($_POST['send'])) {
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
} // closing brace for if(isset($_POST['send'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<fieldset>
<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>
<p class="small"><span class="star">*</span> Denotes a required field </p>
<input type="submit" id="send" name="send" value="Send" />
</fieldset>
</form>
</body>
</html>
Original answer
This line is not properly formatted.
$message = wordwrap($message, 70, "/r/n");
change it to:
$message = wordwrap($message, 70, "\r\n");
You need to use \ instead of /
EDIT
The only way I could get your form to work, is to add a die function.
Try this now:
<?php
function fix_string($var)
{
if(get_magic_quotes_gpc()) $var = stripslashes($var);
$var = strip_tags($var);
return $var;
}
$details = array('name' => fix_string($_POST['name']),
'email' => fix_string($_POST['email']),
'number' => fix_string($_POST['number']),
'message' => fix_string($_POST['message']));
function trim_value(&$value)
{
$value = trim($value);
}
array_walk($details, 'trim_value');
foreach ($details as $field => $detail)
{
if (empty($detail) && $field!='number')
die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");
}
$message = "";
foreach ($details as $field => $detail)
$message .= $field . ": " . $detail . "\n";
$send = $_POST['send'];
$email = $_POST['email'];
$to = "smokey.12345#hotmail.co.uk";
$subject = "Website contact form";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
echo "<p class='success'>Mail was sent successfully</p>";
exit;
?>
This question already has answers here:
How to '$_POST' back to the same page or to a different result page in php? [closed]
(3 answers)
Closed 9 years ago.
What I want to do is to create a simple contact form. I have a file called contact.php with the simple html form:
<form action="process-contact.php" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" value="Enviar">
</form>
And I have this php code:
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email#domain.com";
if($name != '' && $country != '' && $email != '' && $message != ''){
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thank\'s for contacting us. We will be answering back soon.<br><br>Go back.';
}else{
echo 'Plese verify all the fields and try to send the form again.<br><br>Go back.';
}
The php code is in another file, but I would like to have the php code on the same file file. Is that possible? How can I do it?
Thank you.
You can put everything inside one file and using action=""
Your headers had errors in them and by using those, the Email ended up in my SPAM box, so I changed those with proper headers and put other details using the $message variable.
I added header('Location: thank_you.php'); and will redirect to that page if all the fields were filled. Create a file called thank_you.php or change it to go to your home page.
I replaced:
if($name != '' && $country != '' && $email != '' && $message != ''){
by:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
Form and PHP handler (tested successfully)
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
else {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Or you can use this as your PHP handler
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(!empty($_POST['name']) &&
!empty($_POST['email']) &&
!empty($_POST['country']) &&
!empty($_POST['message'])) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thanks for contacting us. We will be answering back soon.<br><br>Go back.';
// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");
}else{
echo 'Please verify all the fields.<br><br>Go back.';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Set the action attribute of the form to the name of the file containing the form and php code and then include all your form processing php code in the following conditional.
if(count($_POST)) > 0)
This will make sure the php code runs only when the form is submitted.
You'll want to use the isset feature of PHP, perhaps.
$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}