I have set up a PHP mail form set up that only correctly outputs some of the variables entered in the form. It DOES mail the $name and $email variables, but not the $message variable.
The php to send the form is here:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" ){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
//sending email
require_once("siteIncludes/class.phpmailer.php");
$mail = new PHPMailer();
$email_body = "";
$email_body = $email_body . "Name: " . $name . $message . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom("$email,$name");
$address = "foo#bar.com";
$mail->AddAddress($address);
$mail->Subject = "Form Submission | ".$name;
$mail->MsgHTML($email_body);
if(!$mail->Send() ){
echo 'There was a problem sending the email: '.$mail->ErrorInfo;
exit();
}
header("Location: myContact.php?status=thanks");
exit();
};
?>
And the HTML that sets up the form is here:
<div id="contactFormWrap" class="span6 offset3">
<form method="post" action="myContact.php" id="contactForm">
<div>
<label for="name">Please leave your name</label>
<input type="text" name="name" id="name" value="" class="required" />
</div>
<div>
<label for="email">and your email</label>
<input type="text" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject">What is your message about?</label>
<input type="text" name="subject" id="subject" value="" class="required" />
</div> -->
<div>
<label for="message">and your message</label>
<textarea name="message" id="message" value="" rows="10" class="required"></textarea>
</div>
<div id="messageButtons">
<input type="submit" value="Submit" name="contactSubmit" id="contactSubmit" class="sendEmail btn" />
</div>
</form>
</div>
I hope that was enough information. Does anyone know why the $message variable isn't being output to the submitted email?
thanks
I think this is your problem:
value=""
The <textarea> tag does not have a value attribute, however different browsers have different ways of handling invalid code, so whatever browser you are using must be using the value found in this invalid attribute instead of what you type in the actual text box.
Just do:
<textarea name="message" id="message" rows="10" class="required"></textarea>
Related
So I've had some issues trying to get my php code to work. I found a form php handler which I want to send the email on the back end for the users.
<?php
if(isset($_POST['submit'])){
ob_start();
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];//Sender first name//
$last_name = $_POST['last_name'];//sender last name//
$subject = "Contact Request.";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$to = "-myemail#outlook.com-"; // this is your Email address
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: http://-mysite-/thankyou.html');
exit();
}
?>
Here is the HTML form info:
<form action="php/email.php" method="post" name = "email form" enctype="multipart/form-data" class="container" align="center">
<label for="firstname"></label>
<strong>*</strong><input type="text" placeholder="Enter First Name" name="first_name" required>
<br>
<label for="lastname"></label>
<strong>*</strong><input type="text" placeholder="Enter Last Name" name="last_name" required>
<br>
<label for="email"></label>
<strong>*</strong><input type="text" placeholder="Enter Email" name="email" required>
<br>
<label for="phoneno"></label>
<input type="phoneno" placeholder="Enter Phone Number" name="phone_number">
<br>
<label for="interested"></label>
<t> Brief description of project:</t><br>
<textarea style="width:100% height:60%" align="center" name="message">
</textarea><br>
<button type="submit" class="btn" value="Send Form"><strong>Submit</strong></button>
</form>
I can't figure out why the page on submit does redirect to the email.php but doesn't do anything from there. Do I need to contact the site support team to restart my php services? I've been smacking my head against the keyboard for the last 2 days as to why this isn't working.
Because you haven't field in your form with name="submit" and trying to get its value in PHP: if(isset($_POST['submit'])).
Bonus tip: don't use name submit for any field of the form, cause some browsers getting fooled with this. Instead use formsubmit, submitted or anything else.
just add this field in your form
<input type="hidden" name="submitted" value="1">
andchange condition in PHP to
if(isset($_POST['submitted']) && intval($_POST['submitted']) == 1)...
So I'm trying to make a contact me form for a website and this is my first time making one of these forms. My issue is that for some odd reason my button is not sending anything and the button doesn't want to work.
This is the input form
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text"class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes"required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Here is my handler.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: .$name. \n".
"email: .$visitor_mail.\n".
"phone: .$phone. \n".
"message: .$message. \n";
$to= "email#gmail.com";
$headers= $email_from.;
$headers .= $visitor_email.;
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>
I'm not sure why this isn't working but if anyone could help me that would be amazing.
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input name="submit" type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Handler
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: {$name} \n" .
"email: {$visitor_email}\n" .
"phone: {$phone} \n" .
"message: {$message} \n";
$to = "email#gmail.com";
$headers = $email_from;
$headers .= $visitor_email;
$res = mail($to, $email_subject, $email_body, $headers);
header("Location: index.html");
}
I found a few errors in your code:
$headers= $email_from.; // syntax error - need to remove dot before ;
$headers .= $visitor_email.; // syntax error - need to remove dot before; same here
In your email body, you used $visitor_mail variable, but your is $visitor_email
Also, I've added the name tag to your form and added an additional validation condition.
Please try to use this solution, I hope it will help you. This solution is working fine on my side
I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Im using this php script to send the contents of my form to my email when the user submits the form, but currently im only able to get the last two fields of my form to send in the email.( message and company name are the only two working) How can I get the entire form to send in the email?
<form role="form" method='post' action='backend/php_mailer.php'>
<input type="text" name='name' class="form-control" id="yourname" placeholder="Name">
<input type="email" name='email' class="form-control" id="email" placeholder="Email">
<input type="text" name='phone' class="form-control" id="phone" placeholder="Phone">
<input type="text" name='company' class="form-control" id="company" placeholder="Company Name">
<textarea name='message' class="form-control" id="message" rows="6" placeholder="Message"></textarea>
<button type="submit" class="btn btn-primary btn-lg ">SUBMIT</button>
</form>
<?php
if (isset($_POST['email']))
{
$userName = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$company = $_REQUEST['company'] ;
$message = $_REQUEST['message'] ;
mail("peaceuponelove#gmail.com", $subject,
$message,$company,$userName );
echo "Thank you";
}
?>
Each argument of mail() has a specific purpose. You cannot just continually pass in arguments and expect them to be appended to the email. You must use string concatenation.
$message = $_REQUEST['message'] . '</br>' . $email . '<br/>' . $phone . '<br/> ' . $userName . '<br/> . ' $company;
mail("peaceuponelove#gmail.com", $subject,$message);
Sidenote You never declared $subject in the code that you've shown. So here's a subject:
$subject = 'Message from '. $userName .' < ' . $email . ' > ';
Here is what i ended up doing to get all fields in email
<?php
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input name="name" type="text" value="" size="30"/><br>
<input name="email" type="text" value="" size="30"/><br>
<input name="phone" type="text" value="" size="30"/><br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$message=$_REQUEST['message'].$email.$phone;
$subject="$name";
mail("peaceuponelove#gmail.com", $subject, $message);
echo "Email sent!";
?>
I'm by no means a web designer/coder, but I have enough general knowledge to get my website going. Just a simple portfolio website.
I'm having some problems with my contact form though. It's strictly CSS, HTML and PHP but when I put in the information (name, email and message) to test it, it sends me an email with those field headings, but no content. For example, the email displays as;
Name:
Email:
Message:
Even when filled out with information.
I have a feeling it's something small and a stupid error, so any help is greatly appreciated!
HTML
<div id="contact-area">
<form method="post" action="contactengine.php">
<input type="text" name="Name" id="Name" placeholder="Name"/>
<input type="text" name="Email" id="Email" placeholder="Email"/>
</form>
</div>
<div id="contact-area2">
<form method="post" action="contactengine.php">
<textarea name="Message" placeholder="Message" rows="20" cols="20" id="Message"></textarea>
</form>
<div id="submit-button">
<form method="post" action="contactengine.php">
<input type="submit" name="submit" value="SUBMIT" class="submit-button" />
</form>
</div>
</div>
PHP
<?php
$EmailFrom = "contact#brettlair.ca";
$EmailTo = "contact#brettlair.ca";
$Subject = "Contact Form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$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=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
}
?>
You have your form fields in one form, and the submit button in another. They need to be in the same form.
<form method="post" action="contactengine.php">
<input type="text" name="Name" id="Name" placeholder="Name"/>
<input type="text" name="Email" id="Email" placeholder="Email"/>
</div>
<div id="contact-area2">
<textarea name="Message" placeholder="Message" rows="20" cols="20" id="Message"></textarea>
<div id="submit-button">
<input type="submit" name="submit" value="SUBMIT" class="submit-button" />
</form>
$Name = Trim(stripslashes($_POST['Name']));
There is no Trim function, should be trim (lowercase).