-5

Basically, I'm trying to create an array, and send the array data to my email.

I'm still learning PHP, and am confused about how to set everything up properly.

If you have any advice that can get me started, I'd really appreciate it.

PHP

<?php

if (isset($_POST['submit'])) {

    $to          = "test@mywebsite.com";
    $subject     = "New Order";
    $name_field  = $_POST['name'];
    $phone_field = $_POST['phone'];


    foreach ($food as $key => $item) {
        $body.= $key." - ".$item ["how_many"]
    }


    $food = array(
    'mexican_torta' => array('how_many' => 2, 'customize' => NO),
    'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
    )
    );

    echo $food['mexican_torta']['how_many'];
}

$body = "Name: $name_field\nPhone: $phone_field\nKey: $key\nItem $item"

echo "Data has been submitted to $to!";
mail($to, $subject, $body);
?>

HTML

<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>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[mexican_torta][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[mexican_torta][customize]'>
</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>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[fish_sandwich][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[fish_sandwich][customize]'>
</div><!-- ITEM_LEFT -->
4

3 回答 3

2

我将专注于您的HTML

<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>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[mexican_torta][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[mexican_torta][customize]'>
</div><!-- ITEM_LEFT -->

这看起来像您正在尝试访问$food,但不是在 PHP 标记内进行。如果您希望您的代码被解析为 PHP,您必须告诉服务器它必须将其解析为 PHP:

<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>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='<?php echo $food['mexican_torta']['how_many']; ?>'> <!-- major difference here -->
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='<?php echo $food['mexican_torta']['customize']; ?>'> <!-- major difference here -->
</div><!-- ITEM_LEFT -->
于 2012-08-22T18:20:13.950 回答
2

$food在循环之后定义,而不是在循环之前定义,并且有一个额外的尾随)

$food = array( 
    // NO is also not defined as of yet, see Matheiu's answer.
    'mexican_torta' => array('how_many' => 2, 'customize' => NO), 
    'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
);

在循环之前,定义您将使用的变量是一个好习惯:

$body = '';
foreach( $food as $key => $item) {

在循环中,您缺少分号:

$body.= $key." - ".$item ["how_many"]; 
                                     ^

而且您的正文声明缺少分号:

$body = "Name: $name_field\nPhone: $phone_field\nKey: $key\nItem $item";
                                                                       ^

最后,最后一条语句覆盖$body了 ,因此循环没有做任何事情。考虑这样的事情:

$body .= "Name: $name_field\nPhone: $phone_field\nKey: $key\nItem $item";

虽然注意$keyand$item将指向数组中的最后一个元素$food,并且$item是一个数组,所以它不会正确地转换为字符串。

于 2012-08-22T18:17:24.747 回答
1

创建数组时出现解析错误。这可能是它不起作用的原因:

$food = array(
  'mexican_torta' => array('how_many' => 2, 'customize' => NO),
  'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
);

并且NO没有定义。您应该保持一致并将其更改为0.

于 2012-08-22T18:14:56.390 回答