php echo htmlspecialchar post error - php

Let me start by saying this is the first time I have touched php since college, and I am very limited in my knowledge. I followed a tutorial in order to implement a contact form on a bootstrap website for a client. Everything works except for the "message" field due to it being a textarea rather than an input. For testing I swapped the textarea with an input, and it worked fine. Is there another method of capturing the user entered text to post? It seems like something simple, but I have been struggling with it for hours, and can't seem to find a solution. I am using the following code for the message html:
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="Enter Your Message" ><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
This is the php I am using, and it results in displaying the error message 'Please enter your message'
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'bot#dtosolutions.com';
$to = 'eric#ericbelldesigns.com';
$subject = 'Message from Contact Form';
$body ="From: $name\n E-mail: $email\n Message:\n $message";
//Check if name is entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
//Check if email is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email';
}
//Check if a message is entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if anti-bot test is correct
if ($human !== 7) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors - send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if(mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message.</div>';
}
}
}

It showing the error message Please Enter you Message because your message is not posting to your php script. According to your code you're checking the message in this line
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
but there are no input or textarea field named message. For posting your message to your php script you should change your textarea name Enter Your Message to message and then it will work perfectly.

I'm not sure why you want to print the message with <?php echo htmlspecialchars($_POST['message']);?>, but I see some possible problems here.
There is not an input with name message. The text area's name is
Enter Your Message. And name with spaces will be converted to
underscores in $_POST. That means you need to refer to
$_POST["Enter_Your_Message"].
I suggest you check if an input is filled before assign its value to a variable.

Related

Post select option PHP

I cannot figure out how to POST select options. The form posts empty values for the contact (option) field. I know the selected_val is incorrect but not sure how to correct.
The form posts fine, but contact returns an empty value. Can anyone help out with how to correct the PHP.
Here's the PHP
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$selected_val = $_POST['contact']; "You have selected :" .$selected_val;
$message = $_POST['message'];
$from = 'From your website';
$to = 'somedomain.com';
$subject = 'Visitor Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\nCity: $city\nContact: $contact\nMessage: $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if business has been entered
if (!$_POST['company']) {
$errBusiness = 'Please enter your business or organization name';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone number';
}
// Check if phone has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter the name of your city';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You!
We will be in touch soon.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
HTML
<form class="form-horizontal form-elements" role="form" method="post" action="services-form.php">
<div class="col-md-8 col-md-offset-2">
<label for="contact">Contact preference</label>
<select class="form-control" id="contact" name="contact">
<option>Email</option>
<option>Phone</option>
</select>
</div>
</form>
value attribute missing It should be like this
<select class="form-control" id="contact" name="contact">
<option>select</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
You need value on option tags.
<option value="email">Email</option>
<option value="phone">Phone</option>
And I don't see any input name submit in your HTML
if( isset($_POST["submit"]) ) {
If so, it's not even go in to your code.
Also this line of your PHP code is doing nothing.
"You have selected :" .$selected_val;
should be
print "You have selected :" .$selected_val;
or
echo "You have selected :" .$selected_val;

My contact form will not validate its input fields. It will submit an email to me while my input fields are blank. Any suggestions?

As stated, my php contact form will send me a blank email message however, it will not validate null input values. Any suggestions? I've tried about everything I know to do.
I'm also wondering if there is a way that I can redirect to a thankyou.html page on submit. I remember the php being something like "header..." to redirect.
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$from = 'Demo Contact Form';
$to = 'email#example.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email";
// Check if name has been entered
if (!$_POST['name']) = 0; {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// If there are no errors, send the email
if (!$errName && !$errEmail) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
HTML...
<!--FORM-->
<form role="form" action="contact.php" method="post" class="form-horizontal align-center">
<div class="form-group col-centered">
<label class="control-label col-sm-4 text-align-center">NAME</label>
<div class="control col-sm-4">
<input type="text" id="name" name="name" class="form-control">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group col-centered">
<label class="control-label col-sm-4 text-align-center">EMAIL</label>
<div class="control col-sm-4">
<input type="text" id="email" name="name" class="form-control">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4 text-align-center">PHONE</label>
<div class="col-sm-4">
<input type="text" name="phone" class="form-control">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="submit" value="submit" class="btnSize btn btn-default btn-lg"><strong>SAVE $150</strong></button>
</div>
</div>
</form>
Thanks for your time.
Here's my take on it:
if (isset($_POST["submit"])) {
$name = !empty( $_POST['name'] ) ? $_POST['name'] : false;
$email = !empty( $_POST['email'] ) ? filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) : false;
if( $name && $email ){
$from = 'Demo Contact Form';
$to = 'email#example.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email";
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}else{
$errors = [];
if( !$name ) $errors [] ='Invalid Name entered';
if( !$email ) $errors [] ='Invalid Email entered';
$result= '<div class="alert alert-danger">'.implode('<br>', $errors ).'</div>';
}
}
filter_var() returns the filtered var or false on failure, so with that and the ternary {if} ? true : false; we can normalize your input and simply everything after that.
We check the inputs as soon as possible, that way we run as little code as needed to get to the end result. We know right away if they are good and can skip the majority of the email code, and output our error message.
Logically we can dump the error flag, an in the else we know one or both inputs are false. The array and implode is just a simpler way of putting in a line break. It's also easier to extend by just adding another input in with minimal code changes. As you can see it would be very easy to add another ternary statement, an and condition and an error message. ( only 2.5 ish lines )
One thing I would do on top of that is this
$post = array_map('trim', $_POST);
And then change all the $_POST to $post, this will account for names added in as spaces or extra white space from Copy and Pasting, a common occurrence.
Further you may be able to minimize the errors like this
}else{
$result= '<div class="alert alert-danger">'.implode('<br>', array_filter([
$name ? false : 'Invalid Name entered',
$email ? false : 'Invalid Email entered',
])).'</div>;
}

Not Receiving Email From PHP Form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am not receiving emails from my php form / html. I am new to making these forms, and I am wondering where I went wrong. The form sets up fine on a live site, and the submit button takes me back to the home page, which is what I want. Would anyone be able to help me figure out why I am not receiving the emails?
Here is the html :
`
<section class="contact-container">
<div class="container mtb">
<div class="row">
<div class="col-md-8 wow fadeInLeft">
<h4>Get in touch</h4>
<hr>
<p>Leave a comment, review, or general musings.</p><br />
<form id="contact_form" action="websitehere" method="post">
<label>Name: <input class="textfield" name="name" type="text" value="" /></label>
<label>Email: <input class="textfield" name="email" type="text" value="" /></label>
<label>Message: <textarea class="textarea" cols="45" name="message" rows="5"></textarea></label>
<input class="button" name="submit" type="submit" value="Submit" />
</form>
</div>
And here is the PHP:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = 'emailaddress#email.com';
$subject = 'Message for the author ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
You forgot human in your html code. Also, make sure you are not sending this email to a Yahoo email because they have problems with php mails since December 4th. If that doesn't fix your problem, this might help you: http://email.about.com/od/emailprogrammingtips/qt/How_to_Send_Email_from_a_PHP_Script.htm

Input form doesn't parse error and displays blank page on submit

EDIT: Changed PHP to recommended code, still can't get success/error to display on page.
I'm trying to get this contact page to work, but it seems to be breaking when I click the submit button. As soon as I click, it redirects me to /contact_form.php and shows a blank page. If all forms have been correctly filled in, the message does get sent. If any of them are wrong or not filled in, the same blank page is shown.
HTML:
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
PHP:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 4) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
}
else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
}
?>
Try this. I havnt tested it, but this should work like a charm. If you still get errors please post them.
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
You have no print or echo statement in your PHP file ...
add echo $result to the end of your PHP file to see the results
Change your success code this. This will print your success or error msg on page reload.
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
Determine if a variable is set:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='í<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
if(isset($result)){
echo $result;}

PHP tags show as plain text inside HTML code

I'm a front end developer and I'm new to PHP. I'm building a contact form for my site using BootStrap3, while the form actually submits the message and I'm able to receive the sent message in my gmail inbox, some of the PHP tags show as plain text in the modal form. How do I make the error messages appear as text, instead of polluting the form with PHP tags?
The Boottrap3 form:
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?><!-- shows all the time as code-->
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?><!-- shows all the time as code-->
</div>
</div>
</form>
The PHP code (index.php)
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'demo#test.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// If there are no errors, send the email
if (!$errName || !$errEmail || !$errMessage || !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Add at least if condition before you display the paragraph containing error message, e.g.:
<?php if ($errName) : ?>
<p class="text-danger"><?php echo $errName ?></p>
<?php endif; ?>
It should do the trick in your example.
And btw: your contact form should be provided as a PHP file. If it's an HTML file, it wont work because it wont get interpreted by PHP.
Try like this
<?php echo "<p class='text-danger'>".$errName."</p>";?>

Categories