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
Related
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
When I submit the form, I get the statement "No arguments Provide!" however, I have a second website running the same exact form and it completely works. The php file is from the Bootstrap Agency template. Here is the code
HTML
<div class="span9">
<form id="contact-form" class="contact-form" action="contact.php">
<p class="contact-name">
<input id="name" type="text" placeholder="Full Name" value="" name="name" />
</p>
<p class="contact-email">
<input id="email" type="text" placeholder="Email Address" value="" name="email" />
</p>
<p class="contact-phone">
<input id="phone" type="text" placeholder="Phone Number" value="" name="phone" />
</p>
<p class="contact-message">
<textarea id="message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea>
</p>
<p class="contact-submit">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</p>
<div id="response">
</div>
</form>
PHP
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'yourname#yourdomain.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Any help will be greatly appriciated.
After digging around for the file, I believe this is the one that works. Here is the code:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['subject']) ||
empty($_POST['message']))
/*
!filter_var($_['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
*/
if(empty($_POST['name'])) {
echo "Please enter your name."; return false;
}
elseif(empty($_POST['email'])) {
echo "Please enter your email."; return false;
}
if(empty($_POST['message'])) {
echo "Please enter your message."; return false;
}
elseif(empty($_POST['subject'])) {
echo "Statement if there is an error"; return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'youname#yourdomain.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $subject";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
You are missing method="post" in your form.
<form id="contact-form" class="contact-form" action="contact.php" method="post">
The default method when sending in a form is GET.
So you need:
<form method='post' id="contact-form" class="contact-form" action="contact.php">
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...
No, I am new to this form!
I have successfully set up a contact form page. It emails me the 'email address' and 'message' from the form fields, but does not send the name filed value, it is just blank.
I cannot see where I have gone wrong. All the field names match up to the PHP file. Any ideas as to what I'm doing wrong?
Here is my code:
<form id="form_28" name="website_query" action="mailform2.php" accept-charset="UTF-8" method="post" target="_self" enctype="multipart/form-data" style="margin:0;position:absolute;left:231px;top:242px;width:343px;height:229px; /*MainDivStyle*/" __AddCode="here">
<!--MainDivStart-->
<!-- HTML Frame - name txt_31 -->
<!--Preamble-->
<div style="position:absolute;left:8px;top:8px;width:51px;height:18px;overflow:hidden; /*BorderDivStyle*/" __AddCode="InsideBorderDiv">
<!--BorderDivContents-->
<p class="Wp-Body-P"><label for="edit_1"><span class="Body-C">name</span></label></p>
</div>
<!--Postamble-->
<!-- Form Edit box edit_1 -->
<!--Preamble-->
<input type="text" id="edit_1" name="name" value="" style="position:absolute; left:103px; top:8px; width:50px; /*Tag Style*/" __AddCode="here">
<!--Postamble-->
<!-- HTML Frame - email txt_32 -->
<!--Preamble-->
<div style="position:absolute;left:8px;top:38px;width:51px;height:18px;overflow:hidden; /*BorderDivStyle*/" __AddCode="InsideBorderDiv">
<!--BorderDivContents-->
<p class="Wp-Body-P"><label for="edit_19"><span class="Body-C">email</span></label></p>
</div>
<!--Postamble-->
<!-- Form Edit box edit_19 -->
<!--Preamble-->
<input type="text" id="edit_19" name="email" value="" style="position:absolute; left:103px; top:38px; width:50px; /*Tag Style*/" __AddCode="here">
<!--Postamble-->
<!-- HTML Frame - message txt_33 -->
<!--Preamble-->
<div style="position:absolute;left:8px;top:68px;width:79px;height:18px;overflow:hidden; /*BorderDivStyle*/" __AddCode="InsideBorderDiv">
<!--BorderDivContents-->
<p class="Wp-Body-P"><label for="text_2"><span class="Body-C">message</span></label></p>
</div>
<!--Postamble-->
<!-- Text Area text_2 -->
<!--Preamble-->
<textarea id="text_2" name="message" rows="2" cols="10" style="position:absolute; left:103px; top:68px; width:100px; height:38px; /*Tag Style*/" __AddCode="here"></textarea>
<!--Postamble-->
<!-- HTML Frame - captcha txt_34 -->
<!--Preamble-->
<div style="position:absolute;left:8px;top:114px;width:69px;height:18px;overflow:hidden; /*BorderDivStyle*/" __AddCode="InsideBorderDiv">
<!--BorderDivContents-->
<p class="Wp-Body-P"><label for="captcha_1"><span class="Body-C">captcha</span></label></p>
</div>
<!--Postamble-->
<!-- Form CAPTCHA captcha_1 -->
<!--Preamble-->
<div style="position:absolute;left:103px;top:114px;width:190px;height:69px; /*MainDivStyle*/" __AddCode="here">
<!--MainDivStart-->
<img src="http://www.serifwebresources.com/util/img_verify.php?gen_word=1&id=captcha_1"><br><input type="text" id="captcha_1" name="captcha_1" size="20" __AddCode="here">
<a title="Listen to audio CAPTCHA" href="http://www.serifwebresources.com/util/audio/audio.php"><img alt="Listen to audio CAPTCHA" style="vertical-align: middle" src="http://www.serifwebresources.com/media/icons/audio-desc.png" border="0"></a><!--MainDivEnd-->
</div>
<!--Postamble-->
<!-- Form Button butn_4 -->
<!--Preamble-->
<input type="submit" style="position:absolute; left:8px; top:191px; width:81px; height:22px; /*Tag Style*/" value="Submit" __AddCode="here">
<!--Postamble-->
</form>
code for PHP (mailform2.php):
<?php
$errors = '';
$myemail = 'myemail';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if( empty($errors))
{
$to = $myemail;
$email_subject = "Website Query: $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);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
$name = $_POST['name'];
In above code if post of name,email and message empty, then it set name = $_POST['name'];
SO $name variable not set.
So code like these
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])) {
$errors = 'Please enter all required fields';
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
}
Okay here i debugged , here is the corrected working code . you have to use isset instead of empty there
<?php
$errors = '';
$myemail = 'myemail';//<-----Put Your email address here.
if(isset($_POST['name']) ||
isset($_POST['email']) ||
isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Website Query: $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";
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Also I covered the if loop you can put mail sending code also inside the if loop there
Hope this helps
if your submiting, try to remove value='' just
<input type="text" id="edit_1" name="name" style="position:absolute; left:103px; top:8px; width:50px; /*Tag Style*/" __AddCode="here">
See if it works !
EDITED & ADDED
change the sumbit's value into name to be: name='submit'
<input type="submit" style="position:absolute; left:8px; top:191px; width:81px; height:22px; /*Tag Style*/" name="submit" __AddCode="here">
and you may wanna change php part to look like this:
<?php
$errors = '';
$myemail = 'myemail';//<-----Put Your email address here.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(isset($_POST['sumbit']) && (
empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])))
{
echo "Please fill up the fields";
}
elseif(isset($_POST['sumbit']) && empty($errors)){
$to = $myemail;
$email_subject = "Website Query: $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);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
if(!isset($_POST['name']) && !isset($_POST['email']) && !isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
}else{
$errors = ''; //process
}
Add your curly braces.
you are missing your curly braces. so the name variable is not being set if every POST variable has a value. The other two are because without the braces the if statement ends after the first statement.
Edit, I think your logic is wrong also. You probably want to say if all of these values are not empty then process.
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;