So I have a from that basically when it submits it runs through what i have on "contact-process.php" and when clicking on submit it will go to that page and then show the success or error, how do I make the submit show the success or error message on the same page and clear the form to show it's sent. here's the code I have on my web page.
<form class="form-horizontal" method="post" action="contact-process.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">
</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#gmail.com">
</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="subject2" name="subject2" placeholder="Logo Design - Request">
</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" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
And then this is the code I have on "contact-process.php"
<?php
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
$errHuman = 'Your anti-spam is incorrect';
}
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
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>";
}
}
?>
Put the html to your PHP script as following:
<form class="form-horizontal" method="post" action="#">
<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">
</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#gmail.com">
</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="subject2" name="subject2" placeholder="Logo Design - Request">
</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" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_POST["name"] != "" AND $_POST["email"] != "" AND $_POST["subject2"] != "" AND $_POST["message"] != "") {
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
echo "<div class='alert alert-danger'>Your anti-spam is incorrect</div>";
}else{
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
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{
echo "<div class='alert alert-success'>Data incomplate</div>";
}
}else{
// do nothing
}
?>
EDIT
action will be:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
OR
action="#"
OR
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
Seek ye first ... then asks
You need to know a little Ajax is a technology that allows you to communicate with php javascript / JSON
Example:
https://scotch.io/tutorials/submitting-ajax-forms-with-jquery
Make a script with alert function then inside the script put location.reload in the success part.
Related
I cant seem to figure out what the problem is with my code. After pressing submit it will redirect to a error page and says "This page isn’t working website is currently unable to handle this request. HTTP ERROR 500"
HTML
<div class="container">
<form action="contact.php" method="POST" class="form">
<div class="form-group">
<label for="name" class="form-label">Your Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Jane Doe" tabindex="1" required>
</div>
<div class="form-group">
<label for="email" class="form-label">Your Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="jane#doe.com" tabindex="2" required>
</div>
<div class="form-group">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="Hello There!" tabindex="3" required>
</div>
<div class="form-group">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" rows="5" cols="50" id="message" name="message" placeholder="Enter Message..." tabindex="4"></textarea>
</div>
<div>
<button type="submit" class="btn">Send Message!</button>
</div>
</form>
</div>
PHP
<?php
$message_sent = false;
if(isset($_POST['email]') && $_POST['email'] != ''){
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messgeSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "email"
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
mail($to,$messgeSubject,$body);
$message_sent = true;
}
}
?>
there is an typing error in your php (line no 3).. you have written
isset($_POST['email]'
it should be
isset($_POST['email'])
Have got a simple HTML form with file upload field:
<form action="<?php bloginfo('template_url'); ?>/demo-contacts.php" method="post" id="sky-form" class="sky-form" enctype="multipart/form-data">
<header>Submit an <strong>Application!</strong></header>
<fieldset>
<div class="row">
<section class="col col-6">
<label class="label">Name</label>
<label class="input"> <i class="icon-append icon-user"></i>
<input type="text" name="name" id="name">
</label>
</section>
<section class="col col-6">
<label class="label">E-mail</label>
<label class="input"> <i class="icon-append icon-envelope-alt"></i>
<input type="email" name="email" id="email">
</label>
</section>
</div>
<div class="row">
<section class="col col-6">
<label class="label">Telephone Number</label>
<label class="input"> <i class="icon-append icon-phone"></i>
<input type="text" name="telephone" id="telephone">
</label>
</section>
<section class="col col-6">
<label class="label">Address</label>
<label class="input"> <i class="icon-append icon-envelope-alt"></i>
<input type="text" name="address" id="address">
</label>
</section>
</div>
<section>
<label class="label">Subject</label>
<label class="input"> <i class="icon-append icon-tag"></i>
<input type="text" name="subject" id="subject" value="Technician Application">
</label>
</section>
<section>
<label class="label">Covering Letter</label>
<label class="textarea"> <i class="icon-append icon-comment"></i>
<textarea rows="4" name="message" id="message"></textarea>
</label>
</section>
<div class="row">
<section class="col col-6">
<label class="label">Attach your CV</label>
<label class="input"> <i class="icon-append icon-file"></i>
<input type="file" name="cv" id="cv">
</label>
</section>
</div>
<input type="hidden" name="recipient" value="<?php the_field('contact_form_recipient'); ?>">
</fieldset>
<footer>
<button type="submit" class="button">Send</button>
</footer>
<div class="message"> <i class="icon-ok"></i>
<p>Your message was successfully sent!</p>
</div>
</form>
And have the following PHP to process the form which worked well prior to adding file upload field:
$EmailFrom = "email address here";
$EmailTo = Trim(stripslashes($_POST['recipient']));
$Name = Trim(stripslashes($_POST['name']));
$Subject = Trim(stripslashes($_POST['subject']));
$Email = Trim(stripslashes($_POST['email']));
$Telephone = Trim(stripslashes($_POST['telephone']));
$Address = Trim(stripslashes($_POST['address']));
$Message = Trim(stripslashes($_POST['message']));
$CV = Trim(stripslashes($_POST['cv']));
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "CV: ";
$Body .= $CV;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL= URL here\">";
}
else{
echo "failed";
}
At present, the e-mail that comes through just states the name of the file that's been uploaded, i'd like to have the file attached to the email, could someone guide me please?
Thanks
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)
I'm trying to create a website and my PHP and html just wont work!! When I input data the website doesnt send the information to my email, and in the input forms, my validation is showing instead of the placeholder!!! please help!!!!:-)
my html is as follows:
<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" placeholder="Preferred Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
</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']); ?>">
</div>
</div>
<div class="form-group">
<label for="number" class="col-sm-2 control-label">Contact Number</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="number" name="number" placeholder="0000000000">
</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>
</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>
my PHP is as follows:
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\`content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
you have not closed the form tag and in contact there is no value given of post paste this code and check and while using html5 validation it will set validation error message using placeholder like please enter+your placeholder
<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" placeholder="Preferred Name" value="<?php echo #htmlspecialchars($_POST['name']); ?>">
</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']); ?>">
</div>
</div>
<div class="form-group">
<label for="number" class="col-sm-2 control-label">Contact Number</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="number" name="number" placeholder="0000000000" value="<?php echo #$_POST['your_contact_filed_name'] ?>">
</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>
</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>
For some reason name, email and message appear empty when I receive an email.
HTML
<form method="post" action="send.php" class="form-horizontal">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-4">
<input id="name" name="name" type="text" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-4">
<input id="email" name="email" type="email" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="message">Message</label>
<div class="col-md-4">
<textarea class="form-control" id="message" name="message"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" type="submit" value="Submit" class="btn btn-outline-inverse btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
PHP Code Simplified example
<?php
$emailto = "example#example.com";
$subject = "Example subject";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$headers = "From: example#example.com\r\n";
$headers .= "Reply-To: example#example.com\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";
$success = mail($emailto, $subject, $body, $headers);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
For some reason variables $name, $email and $message appear empty when I receive an email from this contact form/mail function. The rest is shown correctly.
You also need to close the " in the $body variable.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";
The problem was in my htaccess file and not in the code, I had some lines which were removing php and html extensions for estethics, so its all works fine now.
Try this.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> ".$name." </p>
<p><strong>Email Address: </strong> ".$email." </p>
<p><strong>Poruka: </strong> ".$message." </p>";