我正在使用PHP邮件程序并收集用户提交的信息。某些输入字段是可选的,这意味着某些数据字段将为空。我不想在邮件中打印或提交空行,所以我想测试字符串是否为空。如果是空的,我想跳过它。
例如,在下面的示例中,在第一个表中,当收集信息时,我想测试$ choice1Field以查看它是否为空,如果是,则跳过它,以及$ qty1Field。换句话说,跳过整个部分。
我不知道正确的语法来编写我想要完成的任务。
任何帮助将不胜感激。谢谢。
比尔
<?php
$choice1Field = $_POST['choice1'];
$qty1Field = $_POST['qty1'];
$choice2Field = $_POST['choice2'];
$qty2Field = $_POST['qty2'];
$choice3Field = $_POST['choice3'];
$qty3Field = $_POST['qty3'];
$body = <<<EOD
<table width="50%" border="0" cellspacing="10" cellpadding="0">
<tr bgcolor="#F6EFBA">
<td width="80%" align="left">$choice1Field</td>
<td>$qty1Field</td>
</tr>
<tr bgcolor="#E8E8FF">
<td width="80%" align="left">$choice2Field</td>
<td>$qty2Field</td>
</tr>
<tr bgcolor="#F6EFBA">
<td width="80%" align="left">$choice3Field</td>
<td>$qty3Field</td>
</tr>
Etc., etc., etc.
</table>
EOD;
$headers = "From: $emailField" . "\r\n";
$headers .= "Content-type: text/html" . "\r\n";
$success = mail($mailto, $emailSubject, $body, $headers);
?>
答案 0 :(得分:0)
您可以使用isset()函数:http://php.net/manual/en/function.isset.php
示例:
if(isset($_POST['choice1']) {
//do something with it.
}
答案 1 :(得分:0)
对于可能为空的每个字段,请尝试以下操作:
if (!empty($choice1Field) && !empty($qty1Field)) {
$body .= <<< EOD
<tr bgcolor="#F6EFBA">
<td width="80%" align="left">$choice1Field</td>
<td>$qty1Field</td>
</tr>
EOD;
}