PHP数组回声问题

时间:2010-07-23 04:30:08

标签: php forms

对不起,我有点像新手。

我有一个表格,其中一些输入是数组,一些不是

<input type="text" name="name" value="name1111" />
<input type="text" name="email" value="email1111" />

<input type="text" name="model[]" value="model1111" />
<input type="text" name="serial[]" value="serial1111" />
<input type="text" name="read[]" value="read1111" />

<input type="text" name="model[]" value="model2222" />
<input type="text" name="serial[]" value="seria2222" />
<input type="text" name="read[]" value="read2222" />

<input type="text" name="model[]" value="model3333" />
<input type="text" name="serial[]" value="serial3333" />
<input type="text" name="read[]" value="read3333" />

<!-- model4444 etc.. dynamically added with jquery as needed -->

当我将它们发布到read.php并使用 foreach 时,输出会重复输入名称和电子邮件而不是发布一次。我不知道该怎么做才能使它发挥作用。

这就是我正在尝试的

$name = $_POST['name'];
$email = $_POST['email'];
$model = $_POST['model'];
$serial = $_POST['serial'];
$read = $_POST['read'];

$a = "Meter Readings From Customer - $name with contact $email";

foreach ( $model as $key => $n ) {
$b = "Model - " . $n . "with Serial No. " . $serial[$key] . " has a Reading of - " .    $read[$key] . ". End of Reading\n";

echo $a . $b;
}

我希望它像这样出来

  

来自客户的电表读数 - name1111 ,联系人 email1111

     

模型 - model1111 序列号为 serial1111 的读数为 read1111 。阅读结束

     具有序列号 serial2222 的模型 - model2222 具有 read2222 的读数。阅读结束

     

模型 - model3333 序列号为 serial3333 的读数为<读取3333 。阅读结束

最终目标是将其输入 $ msg 然后邮寄并返回成功消息

if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {

    echo "<fieldset>";          
    echo "<div id='success_page'>";
    echo "<h1>Email Sent Successfully.</h1>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
    echo "</div>";
    echo "</fieldset>";

    } else {

    echo 'ERROR!';

    }
}

如果有人可以帮助我,我将非常感激

2 个答案:

答案 0 :(得分:1)

看起来简单地将一个递增的数字附加到名称的末尾并检查$ _POST数组就更简单了。

类似的东西:

<input type="text" name="model1" value="model1111" />
<input type="text" name="serial1" value="serial1111" />
<input type="text" name="read1" value="read1111" />

$b = "Model - " . $n . "with Serial No. " . $_POST["serial".$key] . " has a Reading of - " .    $_POST["read".$key] . ". End of Reading\n";

答案 1 :(得分:1)

我会像这样构建$msg

$msg = "Meter Readings From Customer - $name with contact $email\n\n";

foreach ($model as $key => $n) {
  $msg .= "Model - " . $n . "with Serial No. " . $serial[$key] . " has a Reading of - " . $read[$key] . ". End of Reading\n";
}

如果您不知道.=运算符,它会将右侧的字符串添加到左侧的$msg变量中。循环完成后,您的$msg应该完整并准备好发送。