PHP数组输出错误

时间:2012-08-20 18:46:41

标签: php html

当我提交表单数据时,我在我的PHP代码的第13行(array_push($ order,$ add_order);)上收到此错误:“警告:为foreach()提供的参数无效...”

使这个PHP代码工作的最佳方法是什么?。

以下是当前电子邮件输出(除姓名和电话号码字段外,所有数据似乎都没有正确发送):

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>

1 个答案:

答案 0 :(得分:0)

如果item在任何地方都没有值,则不会将其发布到接收页面。

使用isset检查值是否存在,并is_array检查它是否为数组。