PHP仅在选中框时返回文本字段数据

时间:2014-04-14 18:37:46

标签: php forms

我正在创建一个简单的电子邮件订单表单,但我缺乏PHP技能。我有4个文本字段,如果选中了复选框,我只希望它们返回

    <input type="checkbox" class="checks" name="cards" id="cards" value="YES" /> Business Cards
<label for="cardname1">Name (exactly as it should appear on card): </label><input type="text" name="CardName1" id="CardName1">
<label for="title1">Title: </label><input type="text" name="title1" id="title1">
<label for="Phone1">Phone: </label><input type="text" name="Phone1" id="Phone1">
<label for="Fax1">Fax: </label><input type="text" name="Fax1" id="Fax1">

我正在回来

$body = "From: $name\n E-Mail: $email\n Phone:\n $phone";

来自表格的另一部分。如果选中此框,则包含附加数据的最简单方法是什么?理想情况下,我想将数据合并为一个变量

$carddata = $Cardname1, $title1, $Phone1, $Fax1

1 个答案:

答案 0 :(得分:1)

只需检查复选框是否已选中。如果是,请将值附加到$body。我用了一点&#34;技巧&#34;使用implode()简化了这一点:

if ($Cards === 'Yes') { // make sure your checkbox ichecked. You need to put the real value here.
    $body .= "\n " . implode("\n ", array($Cardname1, $title1, $Phone1, $Fax1));
}