One-Page Responsive Bootstrap Website Showing PHP Code in Contact Form - php

I am finishing up a ONE-PAGE responsive website for a client. I am using a Bootstrap contact form at the bottom of the HTML page. There is a second Contact.php file to process the form. The PHP code is showing up in the contact form text fields.
I've read and followed the directions for other folks' similar problems, but my client's URL ends in .HTML so I don't want to make the URL end in .PHP to make the contact form work. Is there a workaround to A) get the code to NOT show up in the text fields, and B) process the form without changing the HTML to PHP. Thanks a bunch - this has been driving me nuts for two days now.
HTML DOC.
<!-- FORM SECTION STARTS HERE---- -->
<section id="contact">
<div class="container">
<div class="row">
<div class="col-md-12">
<h3 class="h3-complete-form">Just complete the form below.</h3>
</div>
</div>
</div>
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="row">
<div class="col-sm-3"></div>
<div class="contact-icon">
<i class="fas fa-user"></i>
</div>
<div class="form-inline">
<div class="col-sm-2 form-group">
<label for="name" class="control-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
<?php
echo $errName;?>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="contact-icon">
<i class="fas fa-envelope"></i>
</div>
<div class="form-inline">
<div class="col-sm-2 form-group">
<label for="email" class="control-label">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com">
<?php
echo $errEmail;?>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="contact-icon">
<i class="fas fa-pencil-alt"></i>
</div>
<div class="form-inline">
<div class="col-sm-10 form-group">
<label for="message" class="control-label">Message</label>
<textarea class="form-control" rows="4" name="message"
placeholder="How can I help you?">
<?php
echo $errMessage;?>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3"></div>
<div class="contact-icon">
<i class="fas fa-pencil-alt"></i>
</div>
<div class="form-inline">
<div class="col-sm-10 form-group">
<label for="human" class="control-label">2 + 3 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo $errHuman";?>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4"></div>
<div class="form-inline">
<div class="form-group">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
<!-- Displays an alert to user -->
<div class="row">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</form>
</div>
</div>
<!-- --------- SECTION 8: CONTACT ME ENDS HERE------ -->
PHP DOC
<?php include 'validate.php'; ?>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = "From: ". $name . " <" . $email . ">\r\n";
$to = 'myname#myemailaddress.com';
$subject = 'Message from Contact Form';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your first and last 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 answer 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 can try with .htaccess file to treat .html extension as .php.
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

Related

PHP email script keeps rejecting entry even though checks are passed

I have written a PHP script for a contact form however it is not working, as when I press submit the message "Error with sending the message" pops up instead of the confirmation message (see at the end of the php code). I am at a loss here. The script and html are stored in a single file called "contact.php"
The php script is as follows
<?php
if($_POST["submit"]){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = 'faisalhussien#hotmail.com';
$message = "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 (!$_POST['subject']){
$errSubject = 'Please include the subject of the message';
}
//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 answer is incorrect';
}
if(!$errName && !$errEmail && !$errSubject && !$errMessage && !$errHuman){
if (mail ($to, $subject, $message)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Error with sending the message</div>';
}
}
}
?>
And my HTML for the form is:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact</h1>
<form class="form-horizontal" role="form" method="post" action="contact.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" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</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" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
</div>
<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="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
To test the code I am using MAMP on windows (in case that is where the fault lays)

PHP mailer not showing text-danger alerts, also goes to a blank page when submitted

The PHP contact form modal on my site is sending email, but it isn't displaying error messages if fields are left blank or if the wrong number is put in when verifying whether someone is human or not. Also, after you click submit, it goes to a blank page.
Here's my HTML file:
<!-- Modal -->
<div id="contactModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="contactModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Fill everything out below!</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1">
<form class="form-horizontal" role="form" method="post" action="mailer.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" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</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" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<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="message">
<?php echo htmlspecialchars($_POST[ 'message']);?>
</textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End of Modal body -->
</div>
<!-- End of Modal content -->
</div>
<!-- End of Modal dialog -->
</div>
<!-- End of Modal -->
And here's my PHP file:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'myemail#gmail.com';
$subject = 'Message from porfolio site ';
$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>';
}
}
}
?>
Do y'all know what could be going on here?
Your fields are probably empty strings, which would pass the (!$_POST['field']) test. Instead, use if(!empty($_POST['field'])). This makes sure the field exists, is not 0, null, or an empty string.
For instance:
if (!empty($_POST['name'])) {
$errName = 'Please enter your name';
}

php open a modal dialog when sending a form

I got a problem with my modal dialog when i send my form. i would like to keep the modal dialog when i press the send button and display submitting succeessfully, but it doesn't do it.
This is my form code
<div class="modal fade" id="contact" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" role="form" method="post" action="index.php?page=contact">
<div class="modal-header">
<h4>Contact</h4>
</div>
<div class="modal-body">
<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" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</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" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<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="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</form>
</div>
</div>
When this is my php code
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'your email';
$to = 'example#domain.com';
$subject = 'your subject';
$body = "From: $name\nE-Mail: $email\nMessage:\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, $email)) {
$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 you want to reopen your modal after you submit your form ( if you are not using Ajax ), try using this on your modal page:
$(document).ready(function() {
<?php if (isset($_POST["submit"])): ?>
$('#contact').modal('show');
<?php endif; ?>
});

php contact form alert is not showing

After submitting the contact form it should display "Thank You" but it doesn't display it. And when we don't type the input type the error doesn't show. My website is alzirabarretto.com/webdesigner
<!-- PHP CONTACT FORM -->
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Portfolio Contact Form';
$to = 'alzirabarretto#gmail.com';
$subject = 'My Feedback ';
$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';
}
// 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! 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>';
}
}
}
?>
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email" id="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Subject</label>
<input type="subject" class="form-control" placeholder="Subject" id="sub">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg" id="submit">Send</button>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</form>
$result is out of scope when it is printed. Try defining it in start.
$result="";
Add the following code where you want to print it.
if($result!="")
{
echo $result;
}

Bootstrap contact form not function -php

I had coded html and php but cloudsblack84#gmail.com(mail address) still cannot receive mail form my website ( http://cloudsblack.info/ ) and at the end when I clicked the submit button will go into blank page (http://cloudsblack.info/index.php) . Sorry I am just beginner on web design need to learn a lot from you guys .
HTML PART
<!-- Contact Section -->
<section id="contact" class="contacts-section">
<div class="container content-lg">
<div class="title-v1">
<h2>Contact Us</h2>
<p>I'm always happy to hear from you. Please contact or email me for appointment or service enquiry.</p>
</div>
<div class="row contacts-in">
<div class="col-md-6 md-margin-bottom-40">
<ul class="list-unstyled">
<li><i class="fa fa-home"></i> Kuala Lumpur</li>
<li><i class="fa fa-phone"></i> (6)016-7187764</li>
<li><i class="fa fa-envelope"></i> -removed-email-</li>
</ul>
</div>
<div class="col-md-6">
<form method="post" action="index.php">
<label>Name</label>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input type="text" name="name" class="form-control">
</div>
</div>
<label>Email<span class="color-red">*</span></label>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input type="text" name="email" class="form-control">
</div>
</div>
<label>City</label>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input type="text" name="city" class="form-control">
</div>
</div>
<label>Telephone</label>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input type="text" name="telephone" class="form-control">
</div>
</div>
<label>Interested</label>
<div class="row margin-bottom-20">
<div class="col-md-7 col-md-offset-0">
<input type="text" name="interested" class="form-control">
</div>
</div>
<label>Message</label>
<div class="row margin-bottom-20">
<div class="col-md-11 col-md-offset-0">
<textarea rows="8" name="message" class="form-control"></textarea>
</div>
</div>
<p><button type="submit" name="submit" class="btn-u btn-brd btn-brd-hover btn-u-dark">Send Message</button></p>
</form>
</div>
</div>
</div>
PHP PART
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$telephone = $_POST['telephone'];
$interested = $_POST['interested'];
$message = $_POST['message'];
$submit = $_POST['submit'];
if($submit){
$from = 'From: -removed-domain-'."\r\n";
$to = '-removed-email-';
$subject = "Message from -removed-domain-";
$body = "".
"From: ".$name."\n".
"E-mail: ".$email."\n".
"City: ".$city."\n".
"Telephone: ".$telephone."\n".
"Interested: ".$interested."\n".
"Message: ".$message."\n";
if(mail($to, $subject, $body, $from)){
echo '<p>Your message has been sent!</p>';
}
else{
die('<p>Something went wrong, go back and try again!</p>');
}
}
?>
If you see the message Something went wrong, go back and try again! your mail server is incorrectly configured within php.ini or it has timed-out
If you see Your message has been sent!, check your spam folder.
If all else fails, access your mail server logs through WHM or CPanel or contact your hosting provider
$submit = $_POST['submit'];
You havn't given the <button name='submit'> a value, if($submit){ is false/non-existent in your POST data.
Still not a reason for the blank page, need to look at your apache/php error logs or ask your hosting provider.

Categories