I've been working on a contact form. I have added used the POST method to send it to a set email address. It doesn't seem to be working though. It just runs and stops as if the code is broken. The HTML and PHP are below.
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="name">Your name</label> <input class=
"form-control" id="name" name="name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email"
type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class=
"form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name=
"message" rows="6">
</textarea>
</div>
<div class="submit">
<input class="button button-small" type="submit"
value="Send">
</div>
</form>
<?php
if(isset($_POST['submit'])) {
$to = "gfrazer#hotmail.co.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $name . " " . " wrote the following: " . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header('Location: http://www.google.co.uk');
}
?>
You need to add a name to your submit button. From your snippit, you have no $_POST['submit']:
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email" type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class="form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name="message" rows="6"></textarea>
</div>
<div class="submit">
<!-- ADD name="submit" -->
<input name="submit" class="button button-small" type="submit" value="Send" />
</div>
</form>
Related
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>
I'm trying to check if the form has been submitted. If I press the submit button, the success message will appear but when I check my mailbox I don't see any message. I'm using WampServer. I followed a YouTube tutorial where the guy has the exact same code but for some reason, it doesn't work for me.
<form action="contact.php" method="post" id="myForm">
<div class="md-form">
<i class="fas fa-user prefix grey-text"></i>
<input type="text" required name="name" id="form_name" class="form-control" />
<label for="form_name">Your name</label>
</div>
<div class="md-form">
<i class="fas fa-envelope prefix grey-text"></i>
<input type="email" required name="email" id="form_email" class="form-control" />
<label for="form_email">Your email</label>
</div>
<div class="md-form">
<i class="fas fa-tag prefix grey-text"></i>
<input type="text" name="subject" required id="form-Subject" class="form-control" />
<label for="form_subject">Subject</label>
</div>
<div class="md-form">
<i class="fas fa-pencil-alt prefix grey-text"></i>
<textarea id="form_text" name="message" class="form-control md-textarea" rows="3"></textarea>
<label for="form_text">Your message</label>
</div>
<div class="text-center mt-4">
<button class="btn contact-button text-white" type="submit" name="submit">Submit</button>
</div>
<div id="success_message" class="text-dark text-center"></div>
</form>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "patriciushorny#gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an e-mail from ".$name.".\n\n".$message;
mail($mailFrom, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
?>
<form class="form-area contact-form text-right" action="mail.php" method="post">
<div class="row">
<div class="col-lg-6 form-group">
<input name="name" id="name" placeholder="Enter your name" class="common-input mb-20 form-control" type="text">
<input name="email" id="email" placeholder="Enter email address" class="common-input mb-20 form-control" type="email">
<input name="sub" id="sub" placeholder="Enter subject" class="common-input mb-20 form-control" type="text">
</div>
<div class="col-lg-6 form-group">
<textarea class="common-textarea form-control" name="desc" id="desc" placeholder="Enter Messege" ></textarea>
</div>
<div class="col-lg-12">
<button class="genric-btn primary" value="submit" id="submit" name="submit" style="float: right;">Send Message</button>
</div>
</div>
More Details view this link VIEW FULL CODE
My goal is to make a contact form, where the user would type their name, email and message. The moment they press "Send", me, as the admin, would receive an email with all the information that was typed in before. Right now the email is sent but its empty, for example:
Reply to:
User name:
User email
User message
Also, when I click on "Send message" it gives me the same form but without CSS, there must be a code mistake which I don't know what it is.
// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
<form action="index.php?cmd=contform" method="post">
<div class="form-group">
<label for="name" class="sr-only">Name</label>
<input type="text" class="form-control" id="NomeM" placeholder="Nome">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input type="text" class="form-control" id="EmailMen" placeholder="Email">
</div>
<div class="form-group">
<label for="message" class="sr-only">Messagem</label>
<textarea name="message" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
</div>
</form>
</div>
//File contform.php
<?php
$sql="select * from Mensagem ";
$res=$lig->query($sql);
$lin = $res->fetch_array();
$NomeM = $_POST['NomeM'];
$Email = $_POST['EmailMen'];
$Mensagem = $_POST['Mensagem'];
$email_from = 'admin03#happygreen.pt';
$email_subject = "New Form Submission";
$email_body = "User Name:" .$lin['NomeM']."\n".
"User Email:".$lin['EmailMen']."\n".
"User Message:".$lin['Mensagem']."\n";
$to = "filipajoao1933#gmail.com";
$headers = "De: $email_from \r\n";
$headers .= "Responda a: ".$lin['EmailMen']."\r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: Contactos/contactos.php");
?>
Form fields (input, select, textarea) need a name attribute you can reference in PHP, yours only have ids. Also, the for attribute in labels reference the id of a form field:
// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
<form action="index.php?cmd=contform" method="post">
<div class="form-group">
<label for="NomeM" class="sr-only">Name</label>
<input type="text" class="form-control" name="NomeM" id="NomeM" placeholder="Nome">
</div>
<div class="form-group">
<label for="EmailMen" class="sr-only">Email</label>
<input type="text" class="form-control" id="EmailMen" name="EmailMen" placeholder="Email">
</div>
<div class="form-group">
<label for="Mensagem" class="sr-only">Messagem</label>
<textarea name="Mensagem" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
</div>
</form>
</div>
And your $email_body should be:
$email_body = "User Name:" .$NomeM."\n".
"User Email:".$EmailMen."\n".
"User Message:".$Mensagem."\n";
The email I receive only shows the message, not the email address or phone number or even the name.
It also doesn't show from which email it has been sent.
I should receive whole information from the form, otherwise it wouldn't show me the message, I guess.
Maybe someone has more simple code than this one?
I tried but I'm stuck.
Can someone help me out?
Below is the HTML and PHP code
HTML Code:
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input id="name" class="form-control" placeholder="Name" type="text" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input id="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input id="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
PHP code:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = "$email";//<== update the email address
$email_subject = "New Form submission";
$email_phone = $phone;
$email_body = "You have received a new message from the user ".$name.
"\nHere is the message:\n ".$message.
"\nThe phone number:\n ".$phone.
"The email:\n ".$email.
$to = "info#czwebdesign.be";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');
?>
You also need to use name attribute for other fields as well same like message field, something like:
<input name="email" class="form-control" placeholder="Email" required>
<input name="name" class="form-control" placeholder="NAme" required>
<input name="phone" class="form-control" placeholder="Phone" required>
Otherwise you will get the Undefined Index warning in your script.
It's better to use php error_reporting() in your development mode, this will help to find out all errors and warnings in your code.
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
Try this
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input id="name" class="form-control" placeholder="Name" type="text" name="name" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input id="email" name="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input id="phone" name="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
Look here at the missing name="" attribute in your <input>
Use this code instead:
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input name="name" id="name" class="form-control" placeholder="Name" type="text" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input name="email" id="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input name="phone" id="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
This bootstrap using html/php contact form sends me an email just fine, but without any of the users input information!
The email I received looks empty like this:
Name:
Email:
Subject:
Message:
The html for the form is:
<section id="partner">
<div class="container">
<div class="center wow fadeInDown">
<h2><br>Contact</h2>
<p class="lead">Send a general enquiry here or order your service here.</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
</div><!--/.row-->
</div><!--/.container-->
</section><!--/#contact-page-->
The PHP for the form looks like this:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'heerschapnikki#gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Why isn't it working properly? The message "thank you for contacting us" pops up fine and the email sends through, but non of the users information comes through.
Try to print the values you are sending. So use this:
var_dump( $_POST['name'] );
var_dump( $_POST['email'] );
etc...
Doing this, you will be able to see wether your fields are even posted correctly.
the fields have no id param. in the post you get the "id" nor the "name"
<label>Name *</label>
<input type="text" id="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" id="email" class="form-control" required="required">