我是新手。但是我想将选中的复选框值发送到其他页面。 Iam在下面的代码中说明了我的期望;
php_checkbox.php文件
<!DOCTYPE html>
<html>
<head>
<title>Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css" />
</head>
<body>
<div class="container">
<div class="main">
<center>
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<form action="checkbox_value.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><p>
<input type="checkbox" name="check_list[]"
value="C/C++"><label>C/C++ </label> <p>
<input type="checkbox" name="check_list[]" value="Java">
<label>Java</label> <p>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><p>
<input type="checkbox" name="check_list[]"
value="HTML/CSS"><label>HTML/CSS</label><p>
<input type="checkbox" name="check_list[]"
value="UNIX/LINUX"><label>UNIX/LINUX</label><p>
<input type="submit" name="submit" Value="Submit"/>
<p>
</form>
</div>
</div>
</body>
</html>
checkbox_value.php文件
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";}
for ($x = 1; $x <= $checked_count; $x++) {
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<table style="width:50%">
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
</table>
<?php
}
}
else{
}
}
?>
</body>
</html>
以上代码起作用。问题是当我选择Java和PHP时,两个表都显示了PHP。当我选择3个选项时,最后一个选项将显示在所有表中。我需要的是当我选择PHP,JAVA和UNIX / LINUX,三个选项(PHP,JAVA,UNIX / LINUX)分别显示在表上-PHP在第一个表上,Java在第二个表上,以及UNIX / LINUX在第三个表上。 当我仅选择2(例如Java和PHP)时,我希望Java在第一个表上,而PHP在第二个表上。 请帮忙。
答案 0 :(得分:1)
您必须在循环内部编写行逻辑。
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<table style="width:50%">
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
?>
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
<?php
}
}
else{
}
}
?>
</table>
</body>
</html>
答案 1 :(得分:0)
签出下面的代码。您已经不需要使用foreach()
循环来单独计算元素的数量。
另外,只需循环table
,而不是完整的HTML。
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
//Counting number of checked checkboxes.
//$checked_count = count($_POST['check_list']);
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<?php
foreach($_POST['check_list'] as $selected)
{
?>
<table style="width:50%">
<tr>
<td>Technology</td>
<td><?php echo $selected; ?></td>
</tr>
</table>
<?php
}
}
}
?>
</body>
</html>