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
}
Related
I´m tring to add a success message to my form after you click submit, but i dont know how, i have tried different code from different websites, but they dont work with my code. What line do i need to add to this code please?
<form action="contact.php" method="post" id="contact">
<h1>Contact-me!</h1>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="name#example.com">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea name="message" class="form-control" id="message" rows="3"></textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" value="SEND MESSAGE">Submit</button>
<div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>
I've been having trouble with PHP (new to php) to clear my form fields AFTER a successful submission. I would also like to show an alert that it was successful. Any ideas? Using javascript with the onclick listener for the submit button will not work because it clears the fields before PHP has time to send the data.
Here is my html:
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
Here is my PHP:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
If you do not want to use ajax, please try the following
(assuming the html is known as "form.html", and the php is "contact-form-handler.php")
The php redirection to form.html will clear the input fields (a normal behavior in web-browsers), and the javascript will show the word "successful" as an alert.
Please make further adjustment(s) according to your needs.
form.html
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
<script>
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
</script>
<script>
if (get("result")=="success")
{
alert("Successful");
}
</script>
contact-form-handler.php
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
<script>
window.location.href="form.html?result=success"
</script>
On clicking the submit button it is showing submit is not set after the if condition. I guess this message is showing because the form is not getting submitted. I don't know where I'm going wrong kindly help me fix this issue. I'm herewith attaching a part of my contact.php code and mail.php code. Thank you
contact.php
<form id="contact-form" method="POST" action="mail.php">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>
mail.php
<?php
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
header('Location: https://imatrixautomation.com/contact.php');
exit;
?>
nothing wrong with your code, i guess you doing a direct request to mail.php
try this code, maybe this help your problem
<?php
if($_POST){ // add condition if http request is POST then process your POST request
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
exit; // if POST process has done, script will stop, and code above will not showing
}
?>
<form id="contact-form" method="POST" action="">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>
My bootstrap 4 form is not sending any messages, I've uploaded the index.php and contact.php files on the server and I'm not having any response. Would you be able to help me find the problem?
Landing page with simple form
<form id="#contacts" method="POST" class="form" role="form">
<div class="row">
<div class="col-md-6 form-group">
<input class="form-control" id="name" name="name"
placeholder="Podaj swoje Imię i nazwisko" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="website" name="website"
placeholder="Adres strony www" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="telephone" name="telephone"
placeholder="Podaj swój numer telefonu" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="email" name="email"
placeholder="Podaj swój email" type="email" required/>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Twoja wiadomość"
rows="4"></textarea>
<br>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn btn-primary" type="submit">Wyślij</button>
</div>
</div>
</form>
contact.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$website = $_POST['website'];
$telephone = $_POST['telephone'];
$email_from = 'SEO';
$email_subject = 'Wiadomość kontaktowa z... '
$email_body = "Name: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
"Website: $website.\n";
"Telephone: $telephone.\n";
$to = "biuro#of.pl";
$headers = "From: $email_from r\n";
$headers .= "Reply-To: $email r\n";
mail($to,$email-$email_subject,$email_body,$headers);
header("location: index.php");
?>
Your form is missing Action
<form id="#contacts" method="POST" class="form" role="form" action="contact.php">
I am very new to php and am trying to create a simple, secure and functioning contact form on a webpage. My form is as followed:
<form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<fieldset>
<div class="form-group">
<span class="col-md-1 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-11">
<input id="fname" name="name" type="text" placeholder="Name" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 text-center"><i class="fa fa-envelope-o bigicon"></i></span>
<div class="col-md-11">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-11">
<textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-right">
<button type="submit" class="btn btn-primary btn-lg">Send</button>
</div>
</div>
</fieldset>
and I have the following php file (called mail.php) that I would like to execute when SEND button is pressed.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "some.email#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");?>
Question is; if I have
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
how can I execute the php script in mail.php?