Contact form using html and php - php

I am trying to create a contact form using html and php, but when I submit the form instead of running the php file it displays the php file on my screen. I do not get any error message. This is my code:
HTML
<div class="container">
<h1>Contact Me</h1>
<div class="well">
<p class="lead">
Do you have any question? Want to write for us? Please use the below contact form and send a message. I'll reply you as quick as possible.
</p>
</div>
<div class="contact-form">
<form method="post" action="form.php" class="form-horizontal col-md-8" role="form">
<div class="form-group">
<label for="name" class="col-md-2">Name</label>
<div class="col-md-10">
<input name="name" type="text" class="form-control" id="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-2">Email</label>
<div class="col-md-10">
<input name="email" type="email" class="form-control" id="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="subject" class="col-md-2">Subject</label>
<div class="col-md-10">
<input name="subject" type="subject" class="form-control" id="subject" placeholder="Subject">
</div>
</div>
<div class="form-group">
<label for="message" class="col-md-2">Message</label>
<div class="col-md-10">
<textarea name = "message" class="form-control" id="message" placeholder="Message"></textarea>
</div>
</div>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<div class="form-group">
<div class="col-md-12 text-right">
<button type="submit" class="btn btn-lg btn-primary">Submit your message!</button>
</div>
</div>
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];;
$to = 'me#mariachavez.co';
$subject = $_POST['subject'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, you can send an email to me#mariachavez.co and I'll get in touch soon</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>

You can't execute PHP files without server, if you don't have it, you need to install one, for example WAMP or XAMPP.
If you have one, try restarting it, or , if you haven't, configure HTTP server to execute PHP files.

Related

Bootstrap Contact Form Processing

Have a Bootstrap contact form coded and working with on exception. When the form is submitted it doesn't tell the user the message was sent. I want when the user sends the data it gets process on the same page with a successful message. Can anyone tell me what I am doing wrong or give me a fix?
<form class="form-horizontal" role="form" method="post" action="">
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '3Form';
$to = 'info#gmail.com';
$subject = 'Message from 360 Form ';
$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 Phone
if (!$_POST['phone']) {
$errPhone = 'Please enter a phone Number';
}
//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! we 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>';
}
}
}
?>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
i made a few changes in your code you have to use the isset function in php, and for the error message you should create it in your CSS file, last thing i removed the authentication because i prefer to write a js/jquery .
<body>
<?php if (!isset($_POST['name'])) { ?>
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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>
<?php }
else {
//connect to the db
include 'dbconnect.php';
//collect the name data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '360 Web Fform';
$to = 'info#360websolution.com';
$subject = 'Message from 360 Form ';
//inserting data to the DB
$sql = "INSERT INTO YourTable SET
YourTable fields....
mysqli_query($conn,$sql);
echo "<span class="error"> Your request will be processed shortly</span>";
} ?>
</body>
<form class="form-horizontal" role="form" method="post" action="">
<?php
$result ='';
$errName='';
$errEmail='';
$errPhone='';
$errMessage='';
$errHuman='';
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '3Form';
$to = 'info#gmail.com';
$subject = 'Message from 360 Form ';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (empty($_POST['name'])) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check Phone
if (empty($_POST['phone'])) {
$errPhone = 'Please enter a phone Number';
}
//Check if message has been entered
if (empty($_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! we 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>';
}
}
}
?>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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">
<?=$result;?>
</div>
</div>
</form>
to mailing u need to read here
Send email from localhost running XAMMP in PHP using GMAIL mail server

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 form doesn't send mail – just reloads page

I have a problem with my contact form. When I click the send button it just refreshes the page and doesn't send the mail. I tried so many things but couldn't find a solution. I hope somebody can help me.
Here is the php and contact part:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
$mail = mail('mymail#gmail.com', $subject, $body);
if ($mail==true)
{
header ("Location: index.php?success=1#contact");
}
else
{
header ("Location: index.php?success=2#contact" );
}
}
?>
....
<form id="contact-form">
<div class="row">
<div class="col-md-6">
<form class="form-horizontal" method="post" role="form" action="index.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required="required" />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required="required" />
</div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="service">General Question</option>
<option value="suggestions">Suggestions</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="name">
Message</label>
<textarea id="message" name="message" class="form-control" rows="9" cols="25" required="required" placeholder="CONTACT FORM UNDER CONSTRUCTION! PLEASE USE THE ADRESS AT THE RIGHT/DOWN BELOW."></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" name="submit" value="SEND" class="btn btn-skin pull-right" id="btnContactUs">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php if($_GET['success'] == 1)
{
echo "Thank you. Your message was sent!";
}
elseif ($_GET['success'] == 2)
{
echo "Sorry, but there were error(s) found with the form you submitted. <br/>Please try again.";
}
?>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
The problem is that you have a form tag inside another form tag.
Try to change the form tag what have the id contact-form to a div tag.

how to make php contact page work

Hello I'm in need of someone how can help me with php, my current php code just takes me to a "Page not found" page so if someonw can tell me how to fix that I would be so happy.
here is the Html:
<div>
<div>
<p class="text-center" style="font-family:Aparajita; font-size:1.5vw; padding:2%;">Drop me a line</p>
</div>
<form class="form-horizontal bu_contact col-lg-4" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label glyphicon glyphicon-user"></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label glyphicon glyphicon-envelope"></label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label glyphicon glyphicon-pencil"></label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Your Message"></textarea>
</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">
</div>
</div>
</form>
</div>
The php code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'lars81wolf#gmail.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';
}
//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 && !$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>
';
}
}
}
?>
I would put it in one file name it the same as the target basically and all would be fine.
<?php
// NAME THIS FILE contact.php so that the post will send it to this file
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'lars81wolf#gmail.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';
}
//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 && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
echo '
<div class="alert alert-success">Thank You! I will be in touch</div>
';
} else {
echo '
<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>
';
}
}
} else {
// Put all your stuff that will be the normal page in here
?>
<div>
<div>
<p class="text-center" style="font-family:Aparajita; font-size:1.5vw; padding:2%;">Drop me a line</p>
</div>
<form class="form-horizontal bu_contact col-lg-4" role="form" method="post" action="contact.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label glyphicon glyphicon-user"></label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label glyphicon glyphicon-envelope"></label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label glyphicon glyphicon-pencil"></label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Your Message"></textarea>
</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">
</div>
</div>
</form>
</div>
<?php
}
?>
But do not forget all the HTML conform head and foot.
PS: I did not check for any further problems just for the routine itself. Not sure if this will really mail anything unless you configured php correctly for the use of an MTA through mail function ;)

Unable to receive email from simple ajax php contact

Hi I'm trying develop a simple contact form in php. I have done a lot of research but can't seem to solve this.
Here is the HTML code:
<form id="contact-form" action="contact.php" method="post" class="clearfix">
<div class="contact-box-hide">
<div class="col-sm-6">
<input type="text" class="form-control" id="first_name" name="first_name" required placeholder="First Name">
<span class="first-name-error"></span>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" id="last_name" name="last_name" required placeholder="Last Name">
<span class="last-name-error"></span>
</div>
<div class="col-sm-6">
<input type="email" class="form-control" id="contact_email" name="contact_email" required placeholder="Email Address">
<span class="contact-email-error"></span>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" id="subject" name="contact_subject" required placeholder="Subject">
<span class="contact-subject-error"></span>
</div>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="message" name="message" required placeholder="Message"></textarea>
<span class="contact-message-error"></span>
</div>
<div class="col-sm-2">
<button id="contact-submit" class="btn custom-btn col-xs-12" type="submit" name="submit"><i class="fa fa-rocket"></i></button>
<span id="contact-loading" class="btn custom-btn col-xs-12"> <i class="fa fa-refresh fa-spin"></i> </span>
</div>
</div><!-- /.contact-box-hide -->
<div class="contact-message"></div>
</form><!-- /#contact-form -->
And this is the PHP portion:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$contact_email = $_POST['contact_email'];
$contact_subject = $_POST['contact_subject'];
$message = $_POST['message'];
$subject = "New Message from Website Form";
$submit = $_POST['submit'];
if($submit){
//Prepare Email
$from = 'From: My Website'."\r\n";
$to = 'odeleon#outlook.com';
$subject = "Message from United Passions for Christ.org";
$body = "".
"From: ".$first_name."\n".
"E-mail: ".$contact_email."\n".
"Message: ".$message."\n";
//Send email
if(mail($to, $subject, $body, $from)){
echo '<p>Your message has been sent!</p>';
}
}
?>
he contact form shows up OK and there aren't any php errors when the user hits submit. However the email never appears in my inbox. Any ideas?
Is there something that I'm missing from this?
Check php.ini file configuration, you can find that in /etc/ directory.
Refer this http://www.tutorialspoint.com/php/php_sending_emails.htm
Try
if(isset($_POST['submit'])){
//your code to submit
}

Categories