我正在创建一个html表单,其中包含php,以便从最终用户获取信息。用户提交信息后,将在顶部发布一条消息。如果用户在点击submit后没有在任何字段中输入值,则会在顶部显示一条错误消息,并且已经输入的值应该仍然存在于输入中。
我遇到的问题是如何使复选框保持一致。
我首先发布了我的html:
<label><input type="checkbox" name="interests[]" id="rainGauge" value="Make a Rain Gauge" <?php if(in_array("rainGauge", $_POST['interests'])) echo 'checked'; ?>> Make a Rain Gauge </label>
<br><label><input type="checkbox" name="interests[]" id="thermometer" value="Make a Thermometer"<?php if(in_array("thermometer", $_POST['interests'])) echo 'checked'; ?>> Make a Thermometer </label>
<br><label><input type="checkbox" name="interests[]" id="windsock" value="Make a Windsock"<?php if(in_array("windsock", $_POST['interests'])) echo 'checked'; ?>> Make a Windsock</label>
<br><label><input type="checkbox" name="interests[]" id="lightningMouth" value="Make Lightning In Your Mouth"<?php if(in_array("lightningMouth", $_POST['interests'])) echo 'checked'; ?>> Make Lightning In Your Mouth</label>
<br><label><input type="checkbox" name="interests[]" id="hygrometer" value="Make a Hygrometer"<?php if(in_array("hygrometer", $_POST['interests'])) echo 'checked'; ?>> Make a Hygrometer </label><br>
?
这是我的php
?
//foreach loop to show selected Interests
if ((!empty($_POST["interests"])) && ($stopPost !="Y"))
{
echo "You have chosen the following workshops:";
foreach($_POST["interests"] as $checked => $interests)
{
echo "<p><em>" . $interests ."</em></p>";
}
}
?
我不确定为什么我正在使用的东西不起作用。我在网上搜索过解决方案,这似乎对大多数人都有效,所以我不确定我做错了什么。
转载请注明出处:http://www.jxbyjx.net/article/20230513/1484864.html