How to include default value with input data - php

So I built a php email form that allows users to submit inquiries which I got up and running, except there is one caveat. When the users enter their data and clicks submit, I get an email with the data they inputted but it ONLY shows the inputted data and not the value along with it. For example:
Now this is the email that comes over:
So again, my question is how do I get the input values to show in addition to the users inputted data in the email that's received? Example of what I want:
Company Name: David
Number of Employees: 3
Current Coverage: None
Telephone Number: 123-456-7890
Location: New Jersey
Email Address: test#gmail.com
I hope this makes sense. Here is my email form code below as well:
HTML FORM
<form action="index.php"
method="post"
enctype="multipart/form-data"
name="EmailForm">
<input type="text" name="subject" class="form-
control"
value="New Quote Request" required="required"
hidden>
<label for="name">Company Name</label>
<br>
<input type="text" name="name" class="form-control"
placeholder="Company Name" required="required">
<br>
<label for="employees">Number of Employees</label>
<br>
<input type="number" name="employees" class="form-
control"
placeholder="# of Employees" required="required">
<br>
<label for="coverage">Current Coverage</label>
<br>
<input type="text" name="coverage" class="form-
control"
placeholder="Current Coverage"
required="required">
<br>
<label for="telephone">Telephone Number</label>
<br>
<input type="text" name="telephone" class="form-
control"
placeholder="Telephone #" required="required">
<br>
<label for="email">Email Address</label>
<br>
<input type="email" name="email" class="form-
control"
placeholder="Email Address" required="required">
<br>
<label for="location">Location</label>
<br>
<input type="text" class="form-control"
name="location"
placeholder="Location" required="required">
<br>
<button type="submit" value="submit" class="btn
btn-primary">Submit</button>
<br><br>
</form>
PHP CODE
<?php
// get email from the config file
$config = require_once __DIR__ . '/../config/app.php';
$recipient_email = $config['mail']['to_email'];
// contact information
$company_name = $inputs['name'];
$employees = $inputs['employees'];
$coverage = $inputs['coverage'];
$telephone = $inputs['telephone'];
$location = $inputs['location'];
$contact_email = $inputs['email'];
$subject = $inputs['subject'];
// Email header
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = "To: $recipient_email";
$headers[] = "From: $contact_email";
$header = implode('\r\n', $headers);
$body = $message . "\n" . $company_name . "\n" . $employees . "\n" . $coverage . "\n" . $telephone . "\n" . $location . "\n" . $contact_email;
mail($recipient_email, $subject, $body, $header);

The label text isn't transferred to the server along with the form data, that's not how HTML forms work.
So whatever text you want in your email, you have to include it yourself in the php code which generates that email.

Related

php email from html form

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)...

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

PHP self-emailing form doesn't work

So I've been trying to get a PHP script to send an email to myself when a page form is filled out. An example of the page is found here:
http://tonybenwhite.byethost8.com/WSAWebpage/Action.html
The hosting service DOES allow PHP, and my web dev instructor said the PHP looks correct, but the email won't send. Here's the PHP script and the HTML form:
<?php
if(isset($_POST['send'])){
$to_address="my#email.com";
$subject="WSA Day of Action Entry";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$street1=$_POST['street1'];
$street2=$_POST['street2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$initials=$_POST['initials'];
$message="Name: " .$firstname." ".$lastname."\n";
$message .="Email: " .$email."\n";
$message .="Street: " .$street1."\n";
$message .="Street: " .$street2."\n";
$message .="City: " .$city."\n";
$message .="State: " .$state."\n";
$message .="Zip Code: " .$zip."\n";
$message .="Initials: " .$initials."\n";
$headers = 'From: '.$email."\r\n".
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
'X-Mailer: PHP/' .phpversion();
mail($to_address, $subject, $message, $headers);
}
?>
<form name="infoForm" method="post" action="email.php">
<fieldset class="formfield">
<p class="form">
Are you a registered voter in the state of Washington?<br>
<input type="checkbox" id="yesBox"/> Yes<br><br>
First Name: <input type="text" id="firstName" name="firstName"><br>
Last Name: <input type="text" id="lastName" name="lastName"><br><br>
Email: <input type="text" id="email" name="email" size="30"><br><br>
Street Address: <input type="text" id="street1" name="street1" size="30"><br>
<input type="text" id="street2" name="street2" size="30"><br>
City: <input type="text" id="city" name="city" size="30"><br>
State: <input type="text" id="state" name="state" size="30"><br>
Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
Initials: <input type="text" id="initials" name="initials" size="3"><br><br>
<input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/> <input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/><label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
</p>
</fieldset>
</form>
Any ideas what I'm doing wrong?
Edit: The link you provided does have JS to work in conjunction with your submit button.
Disregard what was posted originally as strikethroughs, but continue reading passed that.
I don't know if this is your full and actual code, whether you have JS to check if all fields are filled, yet this disabled=true in:
<input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/>
The submit button will not be clickable since the submit is disabled / set to true.
This won't prevent mail from doing its job, however some of your form elements do not match your POST variables.
For instance:
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
along with name="firstName" and name="lastName"
those are case-sensitive, therefore they should be changed to name="firstname" and name="lastname" respectively.
Using the following in two seperate files was sent and received successfully. I'm wondering if you are using your entire code inside the same file.
If so, use action="" instead of email.php IF your file isn't named email.php
PHP
<?php
if(isset($_POST['send'])){
$to_address="my#email.com";
$subject="WSA Day of Action Entry";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$street1=$_POST['street1'];
$street2=$_POST['street2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$initials=$_POST['initials'];
$message="Name: " .$firstname." ".$lastname."\n";
$message .="Email: " .$email."\n";
$message .="Street: " .$street1."\n";
$message .="Street: " .$street2."\n";
$message .="City: " .$city."\n";
$message .="State: " .$state."\n";
$message .="Zip Code: " .$zip."\n";
$message .="Initials: " .$initials."\n";
$headers = 'From: '.$email."\r\n".
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
'X-Mailer: PHP/' .phpversion();
mail($to_address, $subject, $message, $headers);
}
?>
HTML form:
<form name="infoForm" method="post" action="email.php">
<fieldset class="formfield">
<p class="form">
Are you a registered voter in the state of Washington?<br>
<input type="checkbox" id="yesBox"/> Yes<br><br>
First Name: <input type="text" id="firstname" name="firstname"><br>
Last Name: <input type="text" id="lastname" name="lastname"><br><br>
Email: <input type="text" id="email" name="email" size="30"><br><br>
Street Address: <input type="text" id="street1" name="street1" size="30"><br>
<input type="text" id="street2" name="street2" size="30"><br>
City: <input type="text" id="city" name="city" size="30"><br>
State: <input type="text" id="state" name="state" size="30"><br>
Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
Initials: <input type="text" id="initials" name="initials" size="3"><br><br>
<input type="submit" name="send" class="inputButton" id="send" value="Submit" />
<input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/>
<label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
</p>
</fieldset>
</form>

PHP power form not sending all fields to email

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!";
?>

PHP mail form - only one variable not submitting

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>

Categories