以php电子邮件的形式选择多个复选框
问题描述:
我想为一个网站设置一个简单的PHP联系表单,我需要一些帮助修改PHP以列出选择菜单中的多个项目,并希望得到帮助。我是一名平面设计师,而不是开发人员,所以很多都是我的头脑。这就是问题所在区域位置:以php电子邮件的形式选择多个复选框
HTML:
<form method="post" action="projectplanner.php">
<label>Name*</label><input class="text" name="name" placeholder="Typ hier uw naam">
<label>Email*</label><input class="text" name="email" type="email" placeholder="Typ hier uw email">
<label>Telefoon*</label><input class="text" name="telefoon" type="tel" placeholder="Telefoon of mobiel">
<label>Organisatie*</label><input class="text" name="organisatie" placeholder="Ik vertegenwoordig..">
<label>Bestaande website (indien van toepassing)</label><input class="text" name="website" placeholder="http://www">
<label>Soort project</label>
<input name="project[]" type="checkbox" value="huisstijl" class="check" />Huisstijl<br />
<input name="project[]" type="checkbox" value="websiteontwerp" class="check"/>Website ontwerp<br />
<input name="project[]" type="checkbox" value="websiteontwikkeling" class="check"/>Website ontwikkeling<br />
<input name="project[]" type="checkbox" value="onlinemarketing" class="check-last"/>Online marketing<br />
<label>Beschrijving van uw project</label>
<textarea class="project_text" name="beschrijving" placeholder="Een globale beschrijving. Doelstelling, publiek, concurenten?"></textarea>
<label>Inspiratie</label>
<textarea class="project_text" name="inspiratie" placeholder="Kopieer links naar website/apps/afbeeldingen die u inspireren"></textarea>
<label>* 2+2= ? (Anti-spam)</label>
<input name="human" placeholder="Antwoord">
<input id="submit" name="submit" type="submit" value="Verzend">
PHP:
<?php
$naam = $_POST['naam'];
$email = $_POST['email'];
$telefoon = $_POST['telefoon'];
$organisatie = $_POST['organisatie'];
$website = $_POST['website'];
$beschrijving = $_POST['beschrijving'];
$project = $_POST['project'];
$inspiratie = $_POST['inspiratie'];
$from = 'From: the website';
$to = '[email protected]';
$subject = 'onderwerp';
$human = $_POST['human'];
$body =
"Van: $name\n
E-Mail: $email\n
Telefoon: $telefoon\n
Organisatie: $organisatie\n
Website: $website\n
Project Soort: $project\n
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
print ("Dank u wel");
/*echo '<p>Uw bericht is verzonden!</p>';*/
} else {
echo '<p>Oeps. Er ging iets fout. Probeer nogmaals.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
/*echo '<p>Uw anti-spam antwoord is niet goed ingevuld.</p>';*/
}
答
这将列出的项目(如果有的话已经被选中),象这样:
项目Soort:websiteontwerp,websiteontwikkeling,onlinemarketing
<?php
// insure form variables exist
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$telefoon = isset($_POST['telefoon']) ? $_POST['telefoon'] : '';
$organisatie = isset($_POST['organisatie']) ? $_POST['organisatie'] : '';
$website = isset($_POST['website']) ? $_POST['website'] : '';
$beschrijving = isset($_POST['beschrijving']) ? $_POST['beschrijving'] : '';
$inspiratie = isset($_POST['inspiratie']) ? $_POST['inspiratie'] : '';
$human = isset($_POST['human']) ? $_POST['human'] : '';
$submit = isset($_POST['submit']) ? true : false;
$project = isset($_POST['project'])
? implode(', ', $_POST['project']) // gather selected checkboxes
: 'Er geen projecten geselecteerd'; // (Unsure of translation)
$from = 'From: the website';
$to = '[email protected]';
$subject = 'onderwerp';
$body =
"Van: $name\n
E-Mail: $email\n
Telefoon: $telefoon\n
Organisatie: $organisatie\n
Website: $website\n
Project Soort: $project\n
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";
if ($submit && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
print ("Dank u wel");
/*echo '<p>Uw bericht is verzonden!</p>';*/
} else {
echo '<p>Oeps. Er ging iets fout. Probeer nogmaals.</p>';
}
} else if ($submit && $human != '4') {
/*echo '<p>Uw anti-spam antwoord is niet goed ingevuld.</p>';*/
}
?>
如果任何复选框是当用户发送形式选择,PHP接收它们作为阵列。函数implode()
将它们全部拉到逗号分隔的字符串中。如果您愿意,可以随意将逗号(和/或其后的空格)更改为其他内容。
希望这会有所帮助。
答
当您添加尾随支架安装到您的字段名称(即:项目[]), PHP将创建一个具有给定名称的数组,减去括号。在你的情况下,最快的解决办法可能是使用foreach循环遍历所有的检查,而像这样:
$body =
"Van: $name\n
E-Mail: $email\n
Telefoon: $telefoon\n
Organisatie: $organisatie\n
Website: $website\n
Project Soort: ";
foreach ($project as $checkbox) {
$body .= "$checkbox, ";
}
$body .= "\n
Omschrijving: $beschrijving\n
Inspiratie: $inspiratie";
你可以像这个'$ _POST ['project']'那样访问你的复选框,运行'var_dump($ _ POST ['project'])'来看看它包含了什么。 – afuzzyllama