我有以下代码的多个复选框(数组):
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Business" /> Business</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Public Affairs / Law" />Public Affairs / Law</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Medicine" />Medicine</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Literature / Writing / Journalism" />Literature / Writing / Journalism</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Technology" />Technology</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Engineering" />Engineering</label>
</div>
提交表单时,我尝试通过以下方式检索所选值:
print_r($_POST['major']);
打印:
Array ( [0] => [1] => )
它检测到两个复选框被勾选,但它以某种方式无法检索出值。我做错了什么?
======================= PHP片段====================== ==
require 'dbconnect.php';
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['major'])){
$top_majors = implode(',', $_POST['top_three_major']);
echo $top_majors;
}else{
$top_majors = '-';
}
}
=======================解决======================= =====
好的,我解决了我的问题。当浏览器准备就绪时我尝试清除表单字段是一个愚蠢的错误,因此,复选框值在能够POST之前被重置。谢谢你们帮助我!特别是学习者学生,我很感激!我希望我能投票给你所有人,但太糟糕了,我的名声太低了。
再次感谢。
答案 0 :(得分:0)
试试这个
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Medicine" />Medicine</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Literature / Writing / Journalism" />Literature / Writing / Journalism</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Technology" />Technology</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Engineering" />Engineering</label>
</div>
答案 1 :(得分:0)
我试过这段代码。有一些报价遗失。添加完毕后效果非常好。
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Business" /> Business</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Public Affairs / Law" />Public Affairs / Law</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Medicine" />Medicine</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Literature / Writing / Journalism" />Literature / Writing / Journalism</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name=major[] value="Technology" />Technology</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Engineering" />Engineering</label>
</div>
- PHP ---
print_r($_POST['major']);
- out put -----
Array ( [0] => Medicine [1] => Literature / Writing / Journalism [2] => Technology )
答案 2 :(得分:0)
试试这个它现在正在运作......
<form action="<?=$_SERVER["PHP_SELF"];?>" method="post">
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Medicine"/>Medicine</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Literature / Writing / Journalism"/>Literature / Writing / Journalism</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Technology"/>Technology</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Engineering"/>Engineering</label>
</div>
<input type="submit" name="submit" value="Submit">
</form>
在php中将此代码写在同一页面上,因为我在$_SERVER["PHP_SELF"]
中写action
表示在同一页面提交表单只是根据需要更改操作。
if(isset($_POST['submit'])) {
echo '<pre>';print_r($_POST['major']);echo '</pre>';
echo '<pre>';print_r($_POST);echo '</pre>';
}
答案 3 :(得分:0)
它正在运作:
<form action="" method="post" >
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Medicine" />Medicine</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Literature / Writing / Journalism" />Literature / Writing / Journalism</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Technology" />Technology</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="major[]" value="Engineering" />Engineering</label>
</div>
<input type="submit">
</form>
<?php
if(isset($_POST['major']))
print_r($_POST['major']);
?>
3th name=major[]"
只需更改为name="major[]"
您的Html语法错误!