I have created a simple Html page index.html where the user can submit a message. An additional PHP file email_form.php is created to capture the form submissions and send the form contents to my email address.
index.html:
<form name="contactform" method="POST" action="email_form.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
email_form.php:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
On the website, when I try to submit a message, it redirects me to a link looking like this: mysite.com/email_form.php with an error saying "page not found". I don't know much about PHP and I can't tell what I'm doing wrong.
First of all you should have a closing form tag </form>
Then:
Paste the php code to the top of your HTML file and
Change the file's extension to .php
Change the form's action to index.php
Your index.php should look something like this:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
<form name="contactform" method="POST" action="index.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
I have resolved the problem by changing headerin email_form.php. Now it looks like this:
header("Location: https://example.com");
And it works fine.
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");
?>
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 currently trying to send an HTML contact form or the contents of it using php mail () function. Just hang up and ask for help.
How can I e.g. Your entered text and the email address specified in the contact form via PHP?
So my code looks currently and it works .. Only I do not know how I the data entered and send it with ..
HTML CODE:
<form class="" action="senden.php" method="post">
<div class="">
<label>Name</label>
<input type="text" name="von" placeholder="Bitte geben Sie Ihren Namen ein" value="">
<label>E-Mail Adresse</label>
<input type="text" name="email" placeholder="Bitte geben Sie Ihre Email-Adresse ein" value="">
<label>Telefonnummer</label>
<input type="tel" name="tel" placeholder="Bitte geben Sie Ihre Telefonnummer ein" value="">
</div>
<div class="">
<label>Nachricht</label>
<textarea placeholder="Bitte geben Sie Ihre Nachricht ein" name="nachricht" rows="8" cols="40"></textarea>
<button type="submit" name="submit" value="">Nachricht Abschicken</button>
</div>
</form>
`
PHP CODE:
<?php
$empfaenger = "mymail#gmail.com";
$betreff = "Mail Function";
$from = "From: Max Reimers <absender#domain.de>";
$text = "Email Email Email Email Text Text Text";
mail($empfaenger, $betreff, $text, $from);
?>
Here is Code:
<form class="form-horizontal" action="" id="contact-form" method="post">
<fieldset>
<legend class="text-center colheader">Email Us</legend>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="name" name="name" type="text" placeholder="Full Name" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="phone" name="phonenumber" type="text" placeholder="Phone" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="subject" name="subject" type="text" placeholder="Subject" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<textarea class="form-control" id="message" name="messag" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button type="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
<?php
if(isset($_POST['submit'])) {
$to = 'ourmail#example.com';
$from = $_POST['email'];
$subject ='Adlivetech Contact Form';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$subject = $_POST['subject'];
$messag = $_POST['messag'];
$message = '<html><body>';
$message .='<table>';
$message .='<tr><td>Name:</td><td>'. $name .'</td></tr>';
$message .='<tr><td>Email:</td><td>'. $email .'</td></tr>';
$message .='<tr><td>Phone:</td><td>'. $phone .'</td></tr>';
$message .='<tr><td>Subject:</td><td>'. $subject .'</td></tr>';
$message .='<tr><td>Message:</td><td>'. $messag .'</td></tr>';
$message .='</table>';
$message .='</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "To: {$to}\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$to}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
mail($from, $subject, $message, $headers);
}
?>
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
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.