所以即时尝试从复选框中插入选中的值,但不起作用。我相信我的问题是他们没有在开始时正确设置。我试图使用一个循环,但我相信我做错了:P
我的“isset”:
if(!empty($_POST))
{
if(empty($_POST['name'])) {die("Please enter a Your Name.");}
if(empty($_POST['ckboxes[]'])) {die("Please check at least one box.");}
if(empty($_POST['ddown'])) {die("Please select a value.");}
if(empty($_POST['txtDate'])) {die("Please type in a date.");}
if(empty($_POST['agdis'])) {die("Please agree or disagree.");}
if(empty($_POST['yn'])) {die("Please select Yes or No.");}
和插入过程:
//Insert for Checkboxes//
/////////////////////////
$query2 = "
INSERT INTO coffee_ckbx (
white,
green,
red,
blue
) VALUES (
:white,
:green,
:red,
:blue
)
";
foreach($_POST['ckboxes'] as $check){array($check);}
$query_params2 = array(
':white' => $check['1'],
':green' => $check['2'],
':red' => $check['4'],
':blue' => $check['5']
);
try
{
// Execute the query to create the user
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
$stmt2 = $db->prepare($query2);
$result2 = $stmt2->execute($query_params2);
}
//kept the catch out and other code due to lengthiness.
这是我的HTML:
<li>
<label for='ck-boxes'>Select from the check boxes: </label>
<input type="hidden" name="ckboxes" value="0" />
<input type='checkbox' id='' name='ckboxes' value='white'/>
<input type='checkbox' id='' name='ckboxes' value='green'/>
<input type='checkbox' id='' name='ckboxes' value='red'/>
<input type='checkbox' id='' name='ckboxes' value='blue'/>
</li>
有什么想法吗? 提前谢谢你:)
答案 0 :(得分:0)
尝试将[]
添加到名称的末尾,这样就会告诉PHP这是一个数组。
<input type="hidden" name="ckboxes[]" value="0" />
<input type='checkbox' id='' name='ckboxes[]' value='white'/>
<input type='checkbox' id='' name='ckboxes[]' value='green'/>
<input type='checkbox' id='' name='ckboxes[]' value='red'/>
<input type='checkbox' id='' name='ckboxes[]' value='blue'/>
同样不引用数字,引号内的内容被视为string
因此,$check['1']
应为$check[1]
。