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.
Related
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.
Hi I've got a problem with submitting my own simple form in Wordpress it always redirects me to the page witch not exists. I use almost all thing for action field in form like (get_permalink,$_SERVER["PHP_SELF"]) and even did it manually.
<?php
if (isset($_POST["email"]) && $_POST["email"]) {
$content = "user email: {$_POST["email"]}; phone : {$_POST['phone']} ";
$subject = "Test email ";
$to = "some#gmail.com";
$status = wp_mail($to,$subject,$content);
}
?>
<div class="center form-wrapper">
<form action="<?php echo get_permalink(); ?>" method="post">
<h2 class="center white form-logo">
Subscribe
</h2>
<input type="text" placeholder="Enter email" name="email">
<input type="text" placeholder="Enter name" name="name">
<input type="text" placeholder="Enter phone number" name="phone">
<input type="hidden">
<input type="submit" name="submit" value="send" >
</form>
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.
i have setup a form than when pressed the image submit button it sends the filled form to the buisness email.
Here is the script i have used for php linked from my html.
<?php
if(isset($_POST['submit'])) {
$emailbody = 'First Name = '.$_POST['Name']."\n"
.'Surname = '.$_POST['SName']."\n"
.'Email = '.$_POST['email']."\n"
.'Contact Number = '.$_POST['tel_no']."\n"
.'Adress Line 1 = '.$_POST['address_line_1']."\n"
.'Adress Line 2 = '.$_POST['address_line_2']."\n"
.'Town/City = '.$_POST['town_city']."\n"
.'County = '.$_POST['county']."\n"
.'Post Code = '.$_POST['Post_code']."\n"
.'Newsletter = '.$_POST['newsletter']."\n"
.'Events = '.$_POST['events']."\n"
mail('My email#gmail.com', 'Order Form', $emailbody);
} else {
header('location: Payment.html');
}
?>
The form on the html has been linked via a action. is there anything wrong with the script?
PS on pressing the image it is then forwarded to paypal payment page.
Cheers
Edit:
Here is the HTML Code:
<form name="form1" method="post" action="PaymentFormmail.php" align="left">
<section>
<h1>Basic Infomation:</h1>
<p>
<label for="SName">First Name:</label>
<input type="text" name="Name" id="Name" style="margin-left:50px;">
</p>
<p>
<label for="SName">Surname:</label>
<input type="text" name="SName" id="SName" style="margin-left:64px;">
</p>
<p>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" style="margin-left:28px;">
</p>
<p>
<label for="tel_no">Contact Number:</label>
<input type="text" name="tel_no" id="tel_no" style="margin-left:14px;">
</p>
</section>
<section>
<h1>Billing Infomation:</h1>
<p>
<label for="address_line_1">Address line 1:*</label>
<input name="address_line_1" type="text" id="address_line_1" align="right" style="margin-left:20px;">
</p>
<p>
<label for="address_line_2">Address Line 2: </label>
<input type="text" name="address_line_2" id="address_line_2"style="margin-left:20px;">
</p>
<p>
<label for="town_city">Town/City:*</label>
<input type="text" name="town_city" id="town_city"style="margin-left:55px;">
</p>
<p>
<label for="county">County:*</label>
<input type="text" name="county" id="county"style="margin-left:73px;">
</p>
<p>
<label for="Post_code">Post Code:*</label>
<input type="text" name="Post_code" id="Post_code"style="margin-left:46px;">
</p>
<h1>News and Events:</h1>
<p>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes">
<label for="newsletter">I wish to receive the monthly newsletter from Monster Computers UK</label>
</p>
<p>
<input name="events" type="checkbox" id="events" value="Yes">
<label for="events">I wish to receive any infomation about upcomming events</label>
</p>
</section>
</form>
<p>By Clicking Pay now you agree to the Terms of Service</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="monstercomputeruk#gmail.com">
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="item_name" id="byoName" value="0">
<input type="hidden" name="amount" id="finalpaypal">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
<input name="submit" type="image" onClick="MM_validateForm('Name','','R','SName','','R','email','','R','address_line_1','','R','town_city','','R','county','','R','Post_code','','R');return document.MM_returnValue" src="images/buttons/Pay Now.png" alt="PayPal — The safer, easier way to pay online." border="0">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
Try changing
if(isset($_POST['submit']))
to
if(isset($_POST['SName']))
I think $_POST['submit'] doesn't work..
If this also fails then it means that the information isn't sent to your script.
In this case show us your html for the form used in the script.
EDIT
Using if(isset($_POST['submit'])) to not display echo when script is open is not working
EDIT AFTER HTML
in your javascript function called MM_validateForm
send your information using jquery POST to your mail script and remove the if clause checking if isset.
$.post("test.php", { name: "John", time: "2pm" } );
http://api.jquery.com/jQuery.post/
See examples section.
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.