I need a very simple, but effective PHP form handler that will work with this form:
<form id="contact" method="post" action="contact_handle.php">
<label for="name">Name:</label>
<input type="text" class="text_style" placeholder="John Doe" name="name" /><br />
<label for="email">Email:</label>
<input type="email" class="text_style" placeholder="email#example.com" name="email" /><br />
<label for="message">Subject:</label>
<textarea class="areawidth" rows="4" name="message" /></textarea><br />
<button id="contactbutton" type="submit">Submit</button>
</form>
This is what I currently have but it doesn't send an email and it doesn't redirect like I intended. And I cant seem to figure out whats going on.
<?php
$invalid = '';
$my_email = 'my#email.com';
// Validate input:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$invalid.= "\n All fields are required";
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate email:
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email))
{
$invalid .= "\n Invalid email address";
}
// Send email if no errors detected:
if( empty($invalid))
{
$to = $my_email;
$subject = "Contact form submission: $name";
$body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message:\n $message";
$headers = "From: $email\n";
$headers .= "Reply-To: $email";
mail($to,$subject,$body,$headers);
//redirect to the thank you page:
header('Location: contact_thanks.php');
}
?>
When it is submitted it takes me to contact_handle.php that displays blank and fails to redirect.
i think the redirect has to be the first output to the browser, so if anything else is being output it won't work.
why don't you just include the thank you page rather than redirecting to it, ie.
include 'contact_thanks.php';
If you're providing all of the code, the issue is that your form is throwing an error (by adding text to $invalid, but there is no code to handle $invalid not being empty. To solve this, add this after the closing } at the end:
echo $invalid;
Related
How can I remove the ugly error caused by PHP? I don't know if the webhost is causing it or if I can fix it with PHP/CSS, I have no email validation in my code The error
<div class="row">
<div class="row">
<div class="input-field col s6">
<input id="email" type="email" class="validate" name="email">
<label for="email" data-error="Geen geldig e-mailadres" data-success="Dit e-mailadres word alleen gebruikt om een antwoord te versturen.">Email</label>
</div>
</div>
</div>
<?php
error_reporting(0);
$errors = '';
$myemail = 'my#mail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "/n fill in all fields";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: bedankt.html');
}
?>
</body>
</html>
This is the full code for the email part, error_reporting(0); does not seem to work for the described problem
Change Your Html Email
From
<input type="email" >
To
<input type="text" >
As I commented, changing the mail type to text in your HTML will remove the validation. You mentioned you want to keep the field type as email. To disable the the HTML5 validation, you need to add novalidate to your form.
As in:
<form method="post" action="/yourAction" novalidate>...</form>
Reference
I'm a newbie to php and html, I've created a form which will accept the inputs from user and will send it to a specified Email id. Everything is working fine but on submitting the form, it goes to another new page.
How this can be replaced with a dialog box as a confirmation message upon submitting the form?
HTML -
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
PHP -
<?php
$errors = '';
$myemail = 'mymail#gmail.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name : $name \n Email : $email_address \n Message : $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
echo "Your Message successfully sent, we will get back to you ASAP.";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
You can submit the form in the same page and can simply show JavaScript alert message to the users.
Below code will help you resolving your issue.
<?php
if(isset($_POST['btnSubmit'])){
$errors = '';
$myemail = 'mymail#gmail.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name : $name \n Email : $email_address \n
Message : $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
//echo "Your Message successfully sent, we will get back to you ASAP.";
}
print("<script>alert('Your Message successfully sent, we will get back to
you ASAP.');</script>");
}
?>
<form method="POST" name="contactform" action="">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit" name="btnSubmit"><br>
</form>
Hope it helps!
It can be done with following jQuery function:
$(document).ready(function() {
$("[name='contactForm']").submit(function() {
if(confirm("You are about to submit the form. Are you sure?")){
window.location = "http://www.google.com/"
};
});
});
But you will be redirected to a blank new page anyways. In order to prevent that you can either use an AJAX submit(simply add AJAX construction inside the function) or do the redirect using JavaScript function I added to jQuery script
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a website and the form in the code is not working up. What can I do to make it work and put it in action. Here is the form code....
<div class="form"; action="mail.php"; method="post">
<input class="input-text" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<input class="input-text" type="text" name="email" value="Your E- mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<textarea class="input-text text-area" name="message" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">Your Message *</textarea>
<input class="input-btn" type="submit" value="send message">
</div>
Here is the PHP code I am using to fetch data from form.
<html>
<body>
<?php
$errors = '';
$myemail = 'myemail#example.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The problem is that after entering data in the form, it is not proceeding any further.
Change the markup so that it is a <form> not a <div> and get rid of the semicolons.
<form class="form" action="mail.php" method="post">
...
</form>
Not a PHP expert. With that said, I've used this system before and it seemed to stop working one day. I can't seem to get emails sent anymore even if I'm not getting any error messages. I don't know what's wrong.
Form page:
<form method="POST" name="contactform" action="contact-form-handler.php">
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
What does this concern?<br>
<select name="options">
<option value="null">--</option>
<option value="IEP">IEP</option>
<option value="Section 504">Section 504</option>
<option value="Other">Other</option>
</select>
<p style="width: 50%">Please include in your message what type of service you are inquiring about. Please be as descriptive as possible so we can help you more efficiently. Thank you.</p>
<label for='message'>Message:</label> <br>
<textarea name="message" cols="45" rows="7"></textarea>
<input type="image" src="img/submit.png" alt="Submit"> <br>
</form>
Handling:
<?php
$errors = '';
$myemail = 'louie540x#gmail.com;';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['options']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$options = $_POST['options'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Regarding a(n): $options \n \n Message: \n \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.php');
}
?>
<?php include 'template/top.php'; ?>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
<?php include 'template/bottom.php'; ?>
$myemail = 'louie540x#gmail.com;';
Should be:
$myemail = 'louie540x#gmail.com';
However that may not be your only problem...
I made captcha by this [tutorial][1],
[1]: http://codechirps.com/how-to-add-a-completely-custom-captcha-to-any-web-form/ but it seems to me it doesn't complete.
I made code but i can send email even if i put wrong answer. I feel that i have to write extra code in php file but i don't know where. Any help greatly appriciated
<div class="modal-body">
<form class="contact" name="contact">
<label class="label" for="name">Имя</label><br>
<input type="text" name="name" class="input-xlarge"><br>
<label class="label" for="email">E-mail</label><br>
<input type="email" name="email" class="input-xlarge"><br>
<label class="label" for="message">Сообщение</label><br>
<textarea name="message" class="input-xlarge"></textarea>
</form>
</div>
<div class="modal-footer">
<p>2 + 3 =</p>
<input type="text" name="captcha" />
<input class="btn btn-warning" type="submit" value="Отправить" id="submit">
Закрыть
<?php
$myemail = '';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$captcha = check_input($_POST['captcha']);
echo "<span class=\"alert alert-success\" >Сообщение отправлено</span><br><br>";
if (!preg_match("/5/", $captcha))
{
show_error("Check your math, Dude");
}
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}?>
Okay, so you need to check the values of your inputs to see if they are valid. If not, you display an error and the mail doesn't get sent. If all the checks pass, the maildoes get sent. So you need to check the $_POST['email'] and the $_POST['captcha'] field (and if you want to, check if the rest isn't empty or whatever).
In php, you can do this like this:
$myemail = "";
if(isset($_POST['name'])){ // check if a form is submitted
if(empty(trim($_POST['name'])) || empty(trim($_POST['message'])) || empty(trim($_POST['email'])) || empty(trim($_POST['captcha'])) ){ // check if values are not empty
echo "Please fill in all the required fields.";
}else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ // check email
echo "Please give a real e-mail address.";
}else if(!preg_match("/5/", $_POST['captcha'])){ // the code provided by your script
echo "Get your math right, dude!";
}else{ // all fields seem to be ok
// sanitize input using htmlspecialchars(), see http://stackoverflow.com/a/5788361/1319187
$name = htmlspecialchars($_POST['email']);
$email = $_POST['email']; // email doesn't need to be sanitized since it's been filtered
$message = htmlspecialchars($_POST['message']);
//Send the mail
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
if(mail($to,$email_subject,$email_body,$headers)){
echo "Succes! Your mail has been sent!";
}else{
echo "Something went wrong with the sending of a mail.";
}
}
}
Should be rather straightforward, you can google some functions if you don't know what they do.
I also have no idea where check_input() comes from. It's not a native PHP function, and the link you provided doesn't show what it does. Also, the regex to check whether the value of the captcha is 5 is a bit stupid, you could just check $_POST['captcha'] == '5'. Also keep in mind you have to randomize these values a bit.