html form radio buttons & php - 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.

Related

How to get POST Form data into an email

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.

How do I import the data from a html 5 form into php, and in some way make it visible to ensure that I didn't make any mistakes?

How do I import the data from a html 5 form into php, and in some way make it visible to ensure that I didn't make any mistakes?
Here is what I currently have:
HTML
<form action="Input.php" method="post">
Zipcode: <input type="text" maxlength="5" name="Zipcode" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
Age: <input type="text" maxlength="3" name="Age" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
<br>
<br>
<input type="radio" name="smoke" value="yes" checked>Yes, I use tobacco products.
<br>
<input type="radio" name="smoke" value="no">No, I do not use tobacco products.
<br>
<br>
<input type="submit" name="submit" value="Submit">
PHP
<?php
echo htmlspecialchars($_POST["Zipcode"])
echo htmlspecialchars($_POST["Current Age"])
echo htmlspecialchars($_POST["sex"])
echo htmlspecialchars($_POST["smoke"])
$zipcode = test_input($_POST["Zipcode"]);
$sex = test_input($_POST["sex"]);
$smoke = test_input($_POST["smoke"]);
$age = test_input($_POST["Current Age"]);
print <h2>Your Input:</h2>
print $Zipcode; <br> $age; <br> $smoke; <br> $sex;
?>
<form action="Input.php" method="post">
Zipcode: <input type="text" maxlength="5" name="Zipcode" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
Age: <input type="text" maxlength="3" name="Age" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
<br>
<br>
<input type="radio" name="smoke" value="yes" checked>Yes, I use tobacco products.
<br>
<input type="radio" name="smoke" value="no">No, I do not use tobacco products.
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?PHP
$zipcode = $_POST["Zipcode"];
$sex = $_POST["sex"];
$smoke = $_POST["smoke"];
$age = $_POST["Age"];
echo '<h2>Your Input:</h2>';
echo $Zipcode . '<br>' . $age . '<br>' . $smoke . '<br>' $sex;
?>

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.

Radio Buttons with PHP Form Handling

I have a basic form that I am submitting using some basic PHP. I have the form submission working great, except that I have a radio button (for preferred method of contact) and I am not sure how to add that in the PHP so that sends in the email. Both radio button options have the same name, so that isn't working as the value. My code is below.
The PHP is as follows:
<?php
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$phone = stripslashes($_POST['phone']);
$contact = stripslashes($_POST['contact']);
$message = stripslashes($_POST['message']);
$form_message = "Name: $name \nEmail: $email \nPhone: $phone \nPreferred Method of Contact: $contact \nMessage: $message";
// Exit process if field "human" is filled (because this means it is spam)
if ( $_POST['human'] ) {
echo 'Tastes Like Spam!'; exit; }
// if it is not filled, submit form
else {
header( "Location: http://www.newurl.com");
mail("myemail#gmail.com", "Email Subject", $form_message, "From: $email" );
}
?>
The HTML for the form is below:
<form method="post" id="form" action="handle_form.php">
<div class="field">
<input type="text" name="human" id="human" class="txt" />
</div>
<div class="field form-inline">
<label class="contact-info" for="txtName">Name*</label>
<input type="text" name="name" id="name" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtEmail">Email*</label>
<input type="text" name="email" id="email" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtPhone">Phone</label>
<input type="text" name="phone" id="phone" class="txt" value=""/>
</div>
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" /> <span>Phone</span>
</div>
<div class="field form-inline">
<textarea rows="10" cols="20" name="message" id="message" class="txt" value=""></textarea>
</div>
<div class="submit">
<input class="submit" type="submit" name="submit" value="Submit Form">
</div>
</form>
Thanks so much for the help!
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" value="email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="phone" /> <span>Phone</span>
</div>
Note the added value attribute.
And the PHP:
$contact = $_POST['contact']
//Will return either "email" or "phone".
You radios need values:
<input class="radio" type="radio" value="email" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" value="phone" name="contact" /> <span>Phone</span>
Just give your radio inputs a value-attribute. This is what will get submitted via POST. You can then access it via $_POST['nameofradio']
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>
Easy! Just add a value to your radio buttons.
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>

Categories