这是我的HTML代码
<label>Subject : </label>
<label><input type="checkbox" name="subject" id="subject[]" value="Maths"/>Maths</label>
<label><input type="checkbox" name="subject" id="subject[]" value=English"/>English</label>
<label><input type="checkbox" name="subject" id="subject[]" value="Tamil"/>Tamil</label>
这是php代码
<label>
<input type="checkbox" name="subject" id="subject[]" <?php if (isset($_POST["subject"]) && $_POST["subject"]=="Maths") echo "checked";?> value = "Maths"/>Maths
</label>
<label>
<input type="checkbox" name="subject" id="subject[]" <?php if (isset($_POST["subject"]) && $_POST["subject"]=="English") echo "checked";?> value = "English"/>English
</label>
<label>
<input type="checkbox" name="subject" id="subject[]" <?php if (isset($_POST["subject"]) && $_POST["subject"]=="Tamil") echo "checked";?> value = "Tamil"/>Tamil
</label>
但不起作用。
答案 0 :(得分:1)
如果您要用相同的名称调用所有复选框,则它们必须是一个数组
<label>
<input type="checkbox" name="subject[]" id="subject[]" <?php if (isset($_POST["subject"][0]) && $_POST["subject"][0]=="Maths") echo "checked";?> value = "Maths"/>Maths
</label>
<label>
<input type="checkbox" name="subject[]" id="subject[]" <?php if (isset($_POST["subject"][1]) && $_POST["subject"][1]=="English") echo "checked";?> value = "English"/>English
</label>
<label>
<input type="checkbox" name="subject[]" id="subject[]" <?php if (isset($_POST["subject"][2]) && $_POST["subject"][2]=="Tamil") echo "checked";?> value = "Tamil"/>Tamil
</label>
答案 1 :(得分:0)
这是正确的代码之一。
<label>Subject : </label>
<label><input type = "checkbox" name = "subject[]" id = "subject" <?php echo (in_array("Maths",$_POST["subject"]))?"checked" : " "?> value = "Maths"/>Maths</label>
<label><input type = "checkbox" name = "subject[]" id = "subject" <?php echo (in_array("Maths",$_POST["subject"]))?"checked" : " "?> value = "English"/>English</label>
<label><input type = "checkbox" name = "subject[]" id = "subject" <?php echo (in_array("Maths",$_POST["subject"]))?"checked" : " "?> value = "Tamil"/>Tamil</label>