How to get POST Form data into an email - php

I am making a form for my customer to fill out when they order a product.
I am trying to make it so when they hit submit it emails the data to my email and it cc's them. The email will have styling in it. I can't figure out how to get it to put the data into the email. I am currently using PHPMailer.
Here is the php file
<?php
$FirstName = $_POST['First_Name'];
$LastName = $_POST['Last_Name'];
$Email = $_POST['Email'];
$Company = $_POST['Company'];
$dochtml = new DOMDocument();
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SetFrom('No-Reply#LDSVacuum.com');
$mail->Subject = 'Request';
$mail->AddAddress('Chase.Price#LDSVacuum.com');
$mail->AddCC($Email);
$strhtml = '
<div align="center">
<font face="arial" color="#336699" size="6">
Request
</font>
</div>
<div>
<font face="arial" color="#336699" size="5">
Customer Information
</font>
<p>
<label for="fname">
<font face="arial" color="#336699" size="4">
First Name:
</font>
</label>
<br />
<input type="text" name="fname" id="fname" size="50">
</p>
<p>
<label for="lname">
<font face="arial" color="#336699" size="4">
Last Name:
</font>
</label>
<br />
<input type="text" name="lname" id="lname" size="50">
</p>
<p>
<label for="email">
<font face="arial" color="#336699" size="4">
Email Address:
</font>
</label>
<br />
<input type="text" name="email" id="email" size="50">
</p>
<p>
<label for="company">
<font face="arial" color="#336699" size="4">
Company Name:
</font>
</label>
<br />
<input type="text" name="company" id="company"size="50">
</p>
</div>
';
$dochtml->loadHTML($strhtml);
$fname = $dochtml->getElementById('fname');
$fname->value=$FirstName;
$lname = $dochtml->getElementById('lname');
$lname->value=$LastName;
$email = $dochtml->getElementById('email');
$email->value=$Email;
$company = $dochtml->getElementById('company');
$company->value=$FirstName;
$mail->Body = $strhtml;
$mail->Send();
if(!$mail->Send()) {
echo 'Unknown Error has Occured. Please try again Later.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}else {
echo 'Thank You, $FirstName . We have emailed your request to Info#LDSVacuum.com . Your email $Email has been cc to the email.';
}
?>
Here is the form
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" name="contactform" autocomplete="off" id="nipplerequest" action="http://localhost/emailform.php">
<p>
<label for='First_Name' class="FG3" width="157">
<span class="red">
*
</span>
First Name:
</label>
<br />
<input type="text" name="First_Name" id="First_Name" class="FG2" size="50" placeholder="John">
<br />
<label for='Last_Name' class="FG3">
<span class="red">
*
</span>
Last Name:
</label>
<br />
<input type="text" name="Last_Name" class="FG2" size="50" placeholder="Johnson">
<br />
<label for='Email' class="FG3">
<span class="red">
*
</span>
Your Email Address:
</label>
<br />
<input type="text" name="Email" id="Email" class="FG2" size="50" placeholder="John_Johnson#company.com">
<br />
<label for='Company' class="FG3">
<span class="red">
*
</span>
Company Name:
</label>
<br />
<input type="text" name="Company" class="FG2" size="50" placeholder="Company Inc.">
<br />
</p>
</form>
<br />
<input type="Submit" value="Submit">

You are:
Creating a string of HTML
Creating a DOM from that string of HTML
Modifying that DOM by setting values
Emailing the original string of HTML and ignoring the DOM you generated
You need to convert the DOM back into HTML ($modified_html = $dochtml->saveHTML()) and send that instead of your original string.

Related

html form radio buttons & php

I've made a html using html, and it contains some radio buttons. How would I know which button is selected? I need to send the selected option to my email, but I don't quite understand how it would be incorporated into my php script. Thank you all so much!
Below is my php script:
<?php
$to = 'dinefour4#gmail.com';
$subject = 'The subject';
$name = $_POST['name'];
$email = $_POST['email'];
$option = $_POST['place']; <-- is this the way to know the selected button?
$mobile = $_POST['mobile'];
$message = <<<EMAIL
$name
$email
$mobile
EMAIL;
$header = '$email';
if($_POST) {
mail($to, $subject, $message, $header);
}
?>
<div id="signup">
<form action="signup.php" method="POST" onsubmit="return validate()">
<label name = "sutitle"> <font size="7"> sign up here!</font></label> <br/>
<label ><font size="5" >Name:</font>
<input type="text" name="name" required ></label><br />
<label ><font size="5">Mobile:</font>
<input type="text" name="mobile" required></label><br />
<label ><font size="5">Email: </font>
<input type="text" name="email" required></label><br />
<label> <font size="5">Where to meet your Friends?</font><br/>
<input type="radio" name="place" value="revelle" > <font size="5">Revelle </font><br>
<input type="radio" name="place" value="muir"> <font size="5">Muir </font><br>
<input type="radio" name="place" value="warren"> <font size="5">Warren </font><br>
<input type="radio" name="place" value="erc" > <font size="5">ERC </font><br>
<input type="radio" name="place" value="village"> <font size="5">The Village </font><br>
</label>
<input type="submit" value="Submit">
</form>
<?php
$to = 'dinefour4#gmail.com';
$subject = 'The subject';
$name = $_POST['name'];
$email = $_POST['email'];
$option = ""
if(isset($_POST['place']))
{
$option = $_POST['place'];
}
$mobile = $_POST['mobile'];
$message = <<<EMAIL
$name
$email
$mobile
EMAIL;
$header = '$email';
if($_POST) {
mail($to, $subject, $message, $header);
}
?>
<div id="signup">
<form action="signup.php" method="POST" onsubmit="return validate()">
<label name = "sutitle"> <font size="7"> sign up here!</font></label> <br/>
<label ><font size="5" >Name:</font>
<input type="text" name="name" required ></label><br />
<label ><font size="5">Mobile:</font>
<input type="text" name="mobile" required></label><br />
<label ><font size="5">Email: </font>
<input type="text" name="email" required></label><br />
<label> <font size="5">Where to meet your Friends?</font><br/>
<input type="radio" name="place" value="revelle" > <font size="5">Revelle </font><br>
<input type="radio" name="place" value="muir"> <font size="5">Muir </font><br>
<input type="radio" name="place" value="warren"> <font size="5">Warren </font><br>
<input type="radio" name="place" value="erc" > <font size="5">ERC </font><br>
<input type="radio" name="place" value="village"> <font size="5">The Village </font><br>
</label>
<input type="submit" value="Submit">
If the radio button is selected, then it will set in '$option' variable. Otherwise '$option' variable will be empty.

Email Form Submits successfully but I do not get the phone number field in the record

I am using a custom optin page with a form , While testing and in work,When I submit the form I get all the details on my email except the phone number , I do not see any problem with the codes , I tried diff things like changing the value and stuff but it did not work, Here is the form code
<div class="form fix ">
<p class="form-text">Fill This Out and See Your <br>Timeshare Report</p>
<form name="contactform" action="mail-script.php" method="POST">
<label for="fname">First Name:
<input type="text" name="fname" id="fname" />
</label><br>
<label for="lname">Last Name:
<input type="text" name="lname" id="lname" />
</label><br>
<label for="email">Email Address:
<input type="text" name="email" id="email" />
</label><br>
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="phone" id="aphone" />
</label><br>
<label for="resort">Resort Name:
<input type="text" name="resort" id="resort" />
</label><br>
<label for="amount">Amount Owed? $:
<input type="number" name="amount" id="amount" />
<p style="font-size: 12px !important;margin-top: -14px;padding-right: 30px;text-align:right;">
If Paid Off Leave Zero, Else Put Amount</p>
</label><br>
<div class="checkbox">
<div class="check-text fix">
<p>I'm Considering To</p>
</div>
<div class="check-one fix">
<input type="checkbox" name="call" id="" value="sell"/> Sell It <br>
<input type="checkbox" name="call" id="" value="buy"/> Buy It <br>
<input type="checkbox" name="call" id="" value="rent "/> Rent It
</div>
<div class="check-two fix">
<input type="checkbox" name="call" id="" value="cancel"/> Cancel Mortgage <br>
<input type="checkbox" name="call" id="" value="ownership"/> End Ownership <br>
<input type="checkbox" name="call" id="" value="give"/> Give It Back
</div>
</div>
<p class="captcha">
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label><br>
<input id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
</p>
<input id="submit" type="submit" value="" />
<p class="submit-text">Ensure all fields are completed and correct, allowing you more benefits, while preventing abuse of our data.</p>
</form>
</div>
</div>
This is the mail script which sends me the email
<?php
/* Set e-mail recipient */
$myemail = "**MYEMAIL**";
/* Check all form inputs using check_input function */
$fname = check_input($_POST['fname'], "Enter your first name");
$lname = check_input($_POST['lname'], "Enter your last name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$resort = check_input($_POST['resort']);
$amount = check_input($_POST['amount']);
$call = check_input($_POST['call']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Resort: $resort
Amount: $amount
Call : $call
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
Here is the online url of the page
http://timesharesgroup.com/sell/index.html
You have two form elements with the same name. The second one overwrites the first one and you never receive it. You also forget your alternate phone number in your PHP code.
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="altphone" id="aphone" />
</label><br>
Your PHP:
$phone = check_input($_POST['phone']);
$altphone = check_input($_POST['altphone']);
Phone : $phone
Alt Phone : $altphone
Resort: $resort
Your alternative phone number is overwriting your phone number:
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="phone" id="aphone" />
^^^^^ here
</label><br>
Changing the name for the alternative number should fix that.

PHP Email Function sending a blank body message with subject & sender email

I am trying to make a quote forum that sends the input to an email. I have PHP sending the email, displaying the subject and the senders email, but it doesn't display the main body of content in the email that is sent.. here is my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<div class="quote_text">Send us some of your information and we will get back to you as soon as possible. Let's get this ball rolling.</div>
<br /><br />
<span class="important">* = required fields</span><br />
<form action="" method="POST" enctype="multipart/form-data">
<table width="814" height="310" border="0">
<tr>
<td width="419">
<input type="hidden" name="action" value="submit">
Your Name:<span class="important">*</span><input name="name" type="text" value="" size="30"/><br /><br />
Your Email:<span class="important">*</span><input name="email" type="text" value="" size="30" placeholder="example#example.com"/><br /><br />
Phone Number:<input name="phone" type="text" value="" size="10" maxlength="12" placeholder="(xxx)-xxx-xxxx" /><br /><br />
Company Name:<input name="company_name" type="text" value="" size="30"/><br /> <br />
</td>
<td width="385">
Address of Installation:<span class="important">*</span>
<input name="install_address" type="text" value="" size="30"/><br /><br />
City:<span class="important">*</span><input name="city" type="text" value="" size="30"/><br /><br />
State:<span class="important">*</span><input name="state" type="text" value="" size="30"/><br /><br />
Zip Code:<span class="important">*</span><input name="zip" type="text" value="" size="30"/><br /><br /><br /><br />
</td>
</tr>
<tr>
<td height="102">
Fence Type Description:<br /><textarea name="description" rows="6" cols="45"></textarea>
Number of Corners:<input name="corners" type="text" value="" size="30"/>
</td>
<td><br />
Linear Feet:<input name="linear" type="text" value="" size="30"/><br />
OR<br />
Acres:<input name="acres" type="text" value="" size="30"/><br /><br />
Number of Gate Openings:<input name="gate_opening" type="text" value="" size="30"/>
</td>
</tr>
</table><br><br>
<input type="submit" value="Send email"/>
</form>
<?php
} else {
// Grab forum elements and push into variables for the email
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$company_name = $_REQUEST['company_name'];
$install_address = $_REQUEST['install_address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$description = $_REQUEST['description'];
$corners = $_REQUEST['corners'];
$linear = $_REQUEST['linear'];
$acres = $_REQUEST['acres'];
$gate_opening = $_REQUEST['gate_opening'];
//Build Email
$message="$name<br />
$email<br />
$phone<br />
$company_name<br />
$install_address<br />
$city<br />
$state<br />
$zip<br />
$description<br />
$corners<br />
$linear<br />
$acres<br />
$gate_opening<br />";
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($install_address="")||($city="")||($state="")|| ($zip=""))
{
echo "Please fill the required fields. Click here to try again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Blue Ridge Fencing - Quote Forum";
mail("info#blueridgefenceco.com", $subject, $message, $from);
echo "Email sent! We will get back to you as soon as possible.";
}
}
?>
Thank you so much for you help!

submitted my php form, but none of the values were sent

For some reason, the options are not showing up in my email. I can get the email to send just fine. I can see the body and all its comments, but none of the entries that the user made. I know I am doing something wrong, but I cannot determine what it is.
Also, feel free to mock me if it looks horrible. :)
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
?>
<td class = "form">
<form action="?" method="get" enctype="text/plain">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>
Change method="get"
to
method="post"
and remove enctype . It's not required in this case.
Also, why is there a ? in your action? You can keep action blank as it is posting to the same page.
To make things even simpler for you, copy paste the below code and run the file again
if(isset($_POST['submit']))
{
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
}
?>
<td class = "form">
<form action="" method="post">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" name="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>

PHP Array Output Error

When I submitted the form data, I received this error on Line 13 (array_push($order,$add_order);) of my PHP code: "Warning: Invalid argument supplied for foreach()..."
What's the best way to get this PHP code working?.
Here is the current email output (None of the data seems to be sending properly except for the Name & Phone number field):
Name: Alex
Phone: 5104545778
Item: Array
Quantity:
Add:
Message:
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "test#mywebsite.com";
$subject = "New Order";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$order = array();
foreach($_POST['item'] as $item => $name) {
if ($_POST['quantity_'.$name] > 0) {
$add_order = array('pretty'=>$_POST['pretty-name_'.$name],'qty'=>$_POST['quantity_'.$name],'message'=>$_POST['message_'.$name]);
array_push($order,$add_order);
}
}
$body = "From: $name_field\nE-Mail: $email_field\n";
$body .= "Their Order:\n";
foreach ($order as $item){
$body .= "--".$item['qty']."x ".$item['pretty']."\n
Extra: ".$item['message']."\n\n";
}
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
}
?>
HTML:
<form method="POST" action="neworder.php">
<div class ="item_left">
<img src="images/mexicantortas.jpg" border="2" width="200px" height="150px"><br>
Mexican Torta - $8.50<input name="item[]" type="hidden" value="torta"/>
<input name="pretty-name_torta" type="hidden" value="Mexican Torta"/><br>
How Many? <input name="quantity_torta" type="text" /><br>
<input name="message_torta" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_LEFT -->
<br />
<div class ="item_center">
<img src="images/fishsandwich.jpg" border="2" width="200px" height="150px"><br>
Fish Sandwich - $8.50<input name="item[]" type="hidden" value="fish"/>
<input name="pretty-name_fish" type="hidden" value="Fish Sandwhich"/><br>
How Many? <input name="quantity_fish" type="text" /><br>
<input name="message_fish" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_CENTER -->
<br />
<div class ="item_right">
<img src="images/hamburgers.jpg" border="2" width="200px" height="150px"><br>
Hamburger w/ Fries - $7.00<input name="item[]" type="hidden" value="hamburger"/>
<input name="pretty-name_hamburger" type="hidden" value="Hamburger"/><br>
How Many? <input name="quantity_hamburger" type="text" /><br>
<input name="message_hamburger" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_RIGHT -->
<br />
<div class="horizontal_form">
<div class="form">
<h2>Place Your Order Now: <font size="3"><font color="#037B41">Fill in the form below, and we'll call you when your food is ready to be picked up...</font></font></h2>
<p class="name">
<input type="text" name="name" id="name" style="text-align:center;" onClick="this.value='';" value="Enter your name"/>
</p>
<p class="phone">
<input type="text" name="phone" id="phone" style="text-align:center;" onClick="this.value='';" value="Enter your phone #"/>
</p>
<p class="submit">
<input type="submit" value="Place Order" name="submit"/>
</p>
</div><!-- FORM -->
</div><!-- HORIZONTAL_FORM -->
</form>
If item has no values anywhere it wont get posted to the receiving page.
Use isset to check if the value exists and is_array to check if it's an array.

Categories