I'm new to PHP, and have spent 10 hours trying to figure this problem out.
The goal is to take all data entered into this order form, and send it to my email via PHP.
I have 2 questions:
1. I can get PHP to send data from a single menu item (example: Mexican Tortas), but how do I get PHP to send data from multiple items (example: Mexican Tortas, Fish Sandwich and Hamburger)?
2. How do I tell PHP to not send data from menu items that don't have the "How Many?" or "Customize It?" text fields filled out?
If you could provide a super simple example (or a link to a learning resource) I would really appreciate it.
Thank you,
Abijah
PHP
<?php
if(isset($_POST['submit'])) {
$to = "test#mywebsite.com";
$subject = "New Order";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$item = $_POST['item'];
$quantity = $_POST['quantity'];
$customize = $_POST['customize'];
}
$body = "Name: $name_field\nPhone: $phone_field\n\nItem: $item\nQuantity: $quantity\nCustomize: $customize";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
?>
HTML
<form action="neworder.php" method="POST">
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
<h1>Mexican Torta - $8.50</h1>
<input name="item" type="hidden" value="Mexican Torta"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/fishsandwich.jpg">
<h1>Fish Sandwich - $8.50</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/hamburgers.jpg">
<h1>Hamburger w/ Fries - $7.00</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<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>
I suggest you use '[]', as it groups multiple input fields to an array.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
$to = "test#mywebsite.com";
$subject = "New Order";
$order = array();
$order['name'] = $_POST['name'];
$order['phone'] = $_POST['phone'];
$food = $_POST['food'];
foreach ($food as $type => $value)
if (strlen($value['quantity']) > 0) // assuming 'customize' is optional
$order[$type] = $value;
print_r($order);
}
?>
<html>
<head>
<title>Order now!</title>
<style>label,input,textarea {display:block}</style>
<body>
<?php // You need enctype for '[]' support' ?>
<form action="" method="post" enctype="multipart/form-data">
<div class ="item">
<label>Mexican Torta - $8.50</label>
<b>How Many?</b>
<input name="food[mexican_torta][quantity]" type="text">
<b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b>
<textarea name="food[mexican_torta][customize]"></textarea>
</div>
<div class ="item">
<label>Fish - $3.50</label>
<b>How Many?</b>
<input name="food[fish][quantity]" type="text">
<b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b>
<textarea name="food[fish][customize]"></textarea>
</div>
<h2>Place Your Order Now:</h2>
<em>Fill in the form below, and we'll call you when your food is ready to be picked up.</em>
<label>Enter your name</label>
<input type="text" name="name">
<label>Enter your phone nr.</label>
<input type="text" name="phone">
<button>Submit</button>
</form>
</body>
User submits (for example I left the fish blank) and you get
Array (
[name] => Fab
[phone] => 1212
[mexican_torta] => Array ( [quantity] => 2 [customize] => Test )
)
Play around a bit with the '[]' to get the array output you really desire. The print_r will show you exactly what you get. From here it is really easy to put the food details in the e-mail
You'll need unique form field names. In other words, you can't have repeated items of the form:
<input name="quantity" type="text"/>
Instead, you'll need unique names, such as:
<input name="quantity_fish" type="text"/>
You can then use PHP to parse $_POST and throw out empty fields if you'd like.
Related
I need to email multiple email addresses from each selection of a drop down box, however everything i have tried is not working.. Here is what I have so far
Im using this in the start of the form
$mailto = $_POST['unit'];
This is what I have for the drop down box
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z- index:18;">
<option value= 'email#email.com'; 'email#email.com'> M17</option>
<option value="email#email.com">M16</option>
</select>
The problems im having
1. on option M17 it only sends to the first email address
2. it sends the email address instead of the unit number (M17)
3. Need to figure out a way to edit the email addresses through a seperate "user friendly" page
</head>
<body>
<div id="space"><br></div>
<div id="container">
<div id="wb_Form1"
<form name="11_Dispatch" method="post" action="<?php echo basename(__FILE__); ?>" enctype="multipart/form-data" id="Form1">
<input type="hidden" name="formid" value="form1">
<div id="wb_Text1" style="position:absolute;left:10px;top:76px;width:111px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Address</span> </div>
<input type="text" id="Editbox1" style="position:absolute;left:131px;top:76px;width:198px;height:23px;line-height:23px;z-index:1;" name="LOC" value="">
<div id="wb_Text2" style="position:absolute;left:10px;top:106px;width:111px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Subdivision</span></div>
<input type="text" id="Editbox2" style="position:absolute;left:131px;top:106px;width:198px;height:23px;line-height:23px;z-index:3;" name="SUB" value="">
<div id="wb_Text3" style="position:absolute;left:10px;top:136px;width:111px;height:16px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Call back # </span></div>
<input type="text" id="Editbox3" style="position:absolute;left:131px;top:136px;width:198px;height:23px;line-height:23px;z-index:5;" name="#" value="">
<div id="wb_Text5" style="position:absolute;left:10px;top:166px;width:111px;height:16px;z-index:6;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Chief Complaint</span></div>
<input type="text" id="Editbox4" style="position:absolute;left:131px;top:166px;width:198px;height:23px;line-height:23px;z-index:7;" name="CC" value="">
<div id="wb_Text6" style="position:absolute;left:10px;top:196px;width:111px;height:16px;z-index:8;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt name</span> </div>
<input type="text" id="Editbox5" style="position:absolute;left:131px;top:196px;width:198px;height:23px;line-height:23px;z-index:9;" name="NAME" value="">
<div id="wb_Text7" style="position:absolute;left:10px;top:226px;width:111px;height:16px;z-index:10;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt Age</span> </div>
<input type="text" id="Editbox6" style="position:absolute;left:131px;top:226px;width:198px;height:23px;line-height:23px;z-index:11;" name="AGE" value="">
<div id="wb_Text9" style="position:absolute;left:10px;top:256px;width:111px;height:16px;z-index:12;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Callers Name</span></div>
<input type="text" id="Editbox7" style="position:absolute;left:131px;top:256px;width:198px;height:23px;line-height:23px;z-index:13;" name="CALLER" value="">
<div id="wb_Text10" style="position:absolute;left:10px;top:286px;width:111px;height:16px;z-index:14;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Identifying Factors</span></div>
<input type="text" id="Editbox8" style="position:absolute;left:131px;top:286px;width:198px;height:23px;line-height:23px;z-index:15;" name="NOTES" value="">
<input type="submit" id="Button2" name="" value="Dispatch" style="position:absolute;left:169px;top:357px;width:96px;height:25px;z-index:16;">
<div id="wb_Text12" style="position:absolute;left:166px;top:30px;width:133px;height:22px;z-index:17;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:19px;"><strong>911 Dispatch</strong></span></div>
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z-index:18;">
<option value="M17">M17 </option>
<option value="M16">M16</option>
<option value="M15">M15</option>
<option value="M14">M14</option>
<option value="M11">M11</option>
<option value="M10">M10</option>
<option value="M07">M07</option>
</select>
</form>
As suggested, don't put your to addresses in the form. This is just basic and the mail() part is right from the manual -> http://php.net/manual/en/function.mail.php (Example #4 Sending HTML email shows multiple email recipients using concatenation however, I am using implosion):
PHP:
if(isset($_POST['unit'])) {
// Store emails in the code or database to draw from:
// Personally I like to store this in an array (if I were to hardcode it)
// Reason being, is that if you suddenly one day get access to a database
// and you draw a bunch of email addresses from that database, those emails
// would likely be listed as an array.
// By storing each email in a same-named array, you basically
// create an easy-to-read mailing list
$units['m17'][] = 'email1#tester.com';
$units['m17'][] = 'email2#tester.com';
$units['m16'][] = 'email3#tester.com';
// if the <select> value of $_POST['unit'] equals 'm17'
// $units[$_POST['unit']] then is the same as $units['m17']
$to = implode(",",$units[$_POST['unit']]);
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com'."\r\n";
// As noted in the PHP Manual, the function implode() takes an array
// and combines the values into a string with a "glue", in this case a comma
// $to would then equal: "email1#tester.com,email2#tester.com" if
// $_POST['unit'] equals 'm17'.
mail($to, $subject, $message, $headers);
}
FORM:
<select name="unit" size="1" id="Combobox1" style="position: absolute; left: 43px; top: 324px; width: 61px; height: 21px; z-index: 18;">
<option value="m17">M17</option>
<option value="m16">M16</option>
</select>
First time posting here. I have a php contact form on a CMS site that is sending me duplicate emails. The problem is intermittent. Sometimes it send two copies, sometimes it sends as many as 8 copies. Sometimes it works normally. The issue started after removing some fields from the form.
The form submits to a separate page that sends the email to 3 recipients. This page also serves as a thank you page that displays the information submitted by the user. "Thanks for contacting us the following information has been received".
The embeds are expresionengine code. Thanks for your help!!!
Here is the form:
<form id="Form1" name="Form1" method="post" action="http://myprocessingpage.com">
<label for="fname">Name:</label> <input type="text" size="42" id="fname" name="fname"><br>
<label for="email">Email Address:</label> <input size="42" type="text" id="email" name="email"><br>
<label for="telephone">Telephone:</label> <input size="42" type="text" id="telephone" name="telephone"><br>
<br>
<div style="float: left; margin-right: 20px; margin-bottom: 30px;">
<label>How did you hear about us?</label> <br>
<input type="hidden" id="arrayfix" name="how[]" value="">
<input type="checkbox" id="website" name="how[]" value="website"> Website<br>
<input type="checkbox" id="referral" name="how[]" value="referral"> Referral<br>
<input type="checkbox" id="tradeshow" name="how[]" value="tradeshow"> Tradeshow<br>
</div>
<div style="float: left; margin-bottom: 30px;">
<label >Shelter Type:</label><br>
<input type="hidden" id="arrayfix2" name="type[]" value="">
<input type="checkbox" id="safe4x6" name="type[]" value="Safe_Room_4X6"> 4'X6' Shelter<br>
<input type="checkbox" id="safe4x8" name="type[]" value="Safe_Room_4X8"> 4'X8' Shelter<br>
<input type="checkbox" id="custom" name="type[]" value="Custom_Size_Safe_Room"> Custom Size Shelter<br>
</div>
<div style="clear: both;"></div>
<label for="question"> Questions or Comments:</label><br> <textarea rows="7" maxlength="500" name="question" id="question" cols="50"></textarea><br>
<br>
<input type="submit" class="btnimage" id="submit" name="submit" value="">
</form>
Here is the processing page (http://myprocessingpage.com):
<?php
if (isset($_POST['fname']))
{
?>
<!DOCTYPE html>
<head>
<title>Thank you for contacting us!</title>
{embed="page/headtags"}
</head>
<body>
{embed="page/header"}
{embed="page/drop"}
<div id="contentbg">
<div id="content">
<?php
$name = $_POST['fname'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$question = $_POST['question'];
$howarray = $_POST['how'];
$howimplode = implode("\n", $howarray);
$how = str_replace("_", " ", "$howimplode");
$typearray = $_POST['type'];
$typeimplode = implode("\n", $typearray);
$type = str_replace("_", " ", "$typeimplode");
//sent to us
$to = "mail1#mail.com, mail2#mail.com, mail3#mail.com";
$subject = "Info Request";
$message = "INFO REQUEST:\n\nName: $name\n\nEmail: $email\n\nTelephone: $telephone\n\nHow they heard about us:\n$how\n\nShelter type:\n$type\n\nQuestions or Comments:\n$question";
$from = "info#mysite.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
<div id="form">
<p style="margin-top: 0px; margin-bottom: 40px; color: green; font-weight: bold;">Thank you for contacting us! The following information has been received:</p>
<div style="margin-left: 30px;">
<?php
echo "<p><b>Name:</b> ".$name."</p>";
echo "<p><b>Email:</b> ".$email."</p>";
echo "<p><b>Telephone:</b> ".$telephone."</p>";
$thankshowimplode = implode("</br>", $howarray);
$thankshow = str_replace("_", " ", "$thankshowimplode");
$thankstypeimplode = implode("</br>", $typearray);
$thankstype = str_replace("_", " ", "$thankstypeimplode");
echo "<p><b>How you heard about us:</b></br> ".$thankshow."</p>";
echo "<p><b>Type of shelter(s):</b></br> ".$thankstype."</p>";
echo "<p style='word-wrap:break-word;'><b>Questions or Comments:</b> ".$question."</p>";
?>
</div>
</div>
</div>
{embed="page/footer"}
</body>
</html>
<?php
}
else
{
?>
<script type="text/javascript">
window.location = 'http://mycontactpage.com';
</script>
<?php
}
?>
If someone double-clicks (or clicks it 8 times) your submit button, your script could be run more than once. Try disabling the submit button on click (using Javascript).
Another, probably unnecessary, solution would be to lock your form script (e.g. using semaphores), create a database entry with the provided info, then check to see if you've sent the same info recently (e.g. within the last five minutes), and don't sent it again if you have.
I'm sure the Javscript disable functionality will suffice :)
Or, if you want to find out what's really going on, you could just create a log file that logs "sent [some distinct piece of data that might tell these posts apart] at [timestamp]". If you see a couple of these, with the same [distinct info] entry and really close timestamp, then your problem is multiple clicks to the submit button.
I am currently trying to make a form validation which checks wether some inputs / a textfield are empty or not. I want the text in a textfield to be saved and entered in the next php file in the textfield again. It works with all inputs but not with the textfield. The site loads but the textfield is just empty.
THank you in anticipation!
HTML PART:
<form id="formularheader" name="form" method="post" action="php/contact.php">
<fieldset id="personenInfo">
<p><label for="message" >Nachricht *</label></p>
<p><textarea name="nachricht" id="message" cols="40" rows="10" required=""></textarea></p>
<div class="terms">
<input type="checkbox" name="terms">
</div>
<p style="font-size: 80%; color: #292929; font-style: italic;">* notwendige Angaben</p>
</fieldset>
<input type="submit" id="submitButton" value="Abschicken">
</form>
PHP PART:
<?php
$message = $_POST['message'];
?>
<form id="formularheader" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<fieldset id="personenInfo">
<p><label for="message">Nachricht *</label></p>
<p><textarea name="nachricht" id="message" value="<? echo $message; ?>" cols="50" rows="10" required=""></textarea></p>
<div class="terms">
<input type="checkbox" name="terms">
</div>
<p style="font-size: 80%; color: #292929; font-style: italic;">* notwendige Angaben</p>
</fieldset>
<input type="submit" id="submitButton" value="Abschicken">
</form>
should be $_POST["nachricht"] (name attribute is taken in account when in a POST)
You need to use the field name as opposed to the ID e.g. $_POST['NAME'] whereas you have $_POST['id']
More than one level of validation can be used:
use the HTML5 tool : add required to each input to deny the user from sending empty input
use if(isset())
I'm trying to pre-populate my form values with variables passed through the URL.I've tried many different solutions, sometimes I don't get an error, the variable just doesn't show up. Any help is appreciated, thanks!
URL Example: website.com/?firstname=john
Code:
<html>
<script type="text/javascript">
function writeform(){
selobj = document.getElementById('selform');
newobj = document.getElementById('newform');
p = document.getElementById('menu').selectedIndex + 1;
a = document.getElementById('menu2').selectedIndex + 1;
if((p < 14 && (a == 1 || a == 2 || a == 3 ||a == 4)) { // write the 1st form
txt = 'Person 1: '+'<input type="text"/><br/>';
txt += 'Person 2: '+'<input type="text"/>';
} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';
}
</script>
<div style="width: 400px; float:left;"> <?php echo $_GET["firstname"]; ?></div>
<div style="width: 400px; float: left;"> <!-- Primary Div-->
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select>
<br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3">
<table style="width:350px; height=350px">
<tr>
<td>
<select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<br/>
</td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
<br/>
<button onclick="writeform();">Search</button></td>
<td>
<div id="selform" style="display:none">
<fieldset>
<div id="newform"></div>
</fieldset>
</div>
</td>
</tr>
</table>
</div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</html>
Test that what you get in $_GET variable by using var_dump($_GET), then use:
echo isset($_GET["firstname"]) ? $_GET["firstname"] : "";
Firstly use print_r($_GET) at the begining of the file to check wether you have the parameters passed.
Then you might want to clean up that mess, because defining $firstname 3 times with the same value just to echo it out makes no sense.
Secondly, you would really like to change those action url as I'm pretty sure it's wrong:
<form action="http://site1.com/upload" method="get">
Thirdly, your input names are name="fname" meanwhile using firstname in $_GET. Not really sure if you will ever relate these two but, whatever.
Some advices:
learn to write code quite more readable than this.
go to jQuery.com and do some research, as it really helps you write less , do more.
CSS doesn't use equal (=) sign as value setter , which in your case is height=350px when it should be height: 350px;.
Give elements some ID's or Classes and use some .css files , it will clean your code more than you can imagine.
You had started wrongly, that's why URL doesn't appering
<script type="text/javascript">
function writeform(){
selobj=document.getElementById('selform');
newobj=document.getElementById('newform');
p=document.getElementById('menu').selectedIndex+1;
a=document.getElementById('menu2').selectedIndex+1;
if((p<14 && (a==1 || a==2 || a==3 ||a==4)){ // write the 1st form
txt ='Person 1: '+'<input type="text"/><br/>';
txt+='Person 2: '+'<input type="text"/>';} else {
document.getElementById('div1').style.display='block';
}
// if(p==2 && a==1){ // write the 2nd form
// txt ='Name: '+'<input type="text"/><br/>';
// txt+='Addr: '+'<input type="text"/>';}
newobj.innerHTML=txt;selobj.style.display='block';}
</script>
<body>
<form action="http://site1.com/upload" method="get">
<?php echo $_GET["firstname"]; ?>
<p style="font-size: 16px; font-weight: bold;">Select Something</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu" size="14">
<option selected="selected"><b>--- Common Options ---</b></option>
<option></option> //NY
</select><br/>
<p style="font-size: 16px; font-weight: bold;">Range</p>
<div class="fancy3"><table style="width:350px; height=350px">
<tr><td><select id="menu2" size="4">
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option></select><br/>
</td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
<br/>
<button onclick="writeform();">Search</button></td>
<td><div id="selform" style="display:none">
<fieldset><div id="newform"></div></fieldset></div>
</td></tr></table></div>
</div> <!-- Primary Div closing tag-->
<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">
<form action="http://site1.com/upload" method="get">
First Name: <input name="fname" type="text" value="" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<div id="div1" style="display:none;">
<?php
$firstname = $_GET["firstname"];
?>
First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
Last Name: <input name="lname" type="text" />
Address: <input name="address" type="text" />
Zip Code: <input name="zip" type="text" />
State: <input name="state" type="text" />
<input type="submit" />
</form>
</div>
<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</body>
</html>
A couple of problems that I'm seeing here ... for one you have multiple elements with the same id (see id="selform").
To load get variable into a text input the pattern is like this:
<input type='text' name='fieldname' value='<?= isset($_GET['field'])?$_GET['field']:"") ?>'/>
for a checkbox or radio control it is like this
<input type='checkbox' name='fieldname' value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"checked=\"checked\"":"") />
for select boxes it you do this:
<select name='fieldname'>
<option value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"selected=\"selected\":"" ?>>My Val Label</option>
<option value='myval2' <?= isset($_GET['field']) && $_GET['field'] == 'myval2'?"selected=\"selected\":"" ?>>My Val2 Label</option>
</select>
Here is a nifty select box function that will allow you too more concisely output a select in your code (i find the check with every element a little tedious)
function showSelect($name, $options, $selected, $attr = array()){
$str = "<select name='".$name.'"';
foreach($attr as $name=>$val){
$str.= " ".$name."='".$val."'";
}
$str.=">";
foreach($options as $k=>$val){
$str.= "<option value='".$val."'".($val==$selected?" selected='selected'":"").">".$k.'</option>';
}
$str.="</select>";
}
and you can use it like this...
$days = array();
for($d = 1; $x<=31; $x++){
$days[(string)$d] = (string)$d;
}
echo showSelect("formDays", $days, $_POST["formDays"], array("id"=>"formDays"))
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.