[英]passing checked checkbox value from one page to another page in php
如何在PHP中将复选框值从一个页面发送到另一个页面? 复选框值是从数据库中获取的。 我需要将选中的值从一页发送到另一页。 我是 php 新手。
页面1.php
<form method="post" action="setupstates.php">
<table border=".3px" id='acttable'>
<tr>
<td><b>Act Name</b></td>
<td colspan="5"><input type='text' name="actname" size="130" required></td>
</tr>
<tr>
<td><b>Industry Type</b></td>
<td colspan="5"><input type="checkbox">
Select All Industries</td>
<?php
$conn = get_dbconnect();
$sql1 = "SELECT*FROM industry";
$result = mysqli_query($conn, $sql1);
$nrows = mysqli_num_rows($result);
//echo $nrows;
$sql = "SELECT name FROM industry"; //selection query
$rs = mysqli_query($conn, $sql); //odbc_exec($conn,$sql);
// if(mysqli_num_rows($rs)>0){
while ($nrows > 0) {
echo "<tr>";
for ($i = 0; $i < 5; $i++) {
$row = mysqli_fetch_assoc($rs);
echo "<td><input type='checkbox' name='industrytype' value='" . $row['name'] . "'/>" . $row['name'];
}
$nrows = $nrows - 5;
echo '</tr>';
}
?>
</tr>
</table>
<input type="submit">
</form>
page2.php( setupstates.php )
<table>
<tr>
<td><b>Industry Type</b></td>
<td><input type="checkbox" value="<?php print $_POST["industrytype"] ?>"></td>
</tr>
</table>
试试这个。 它会为你工作
page1.php
<form method="post" action="setupstates.php">
<table border=".3px" id='acttable'>
<tr>
<td><b>Act Name</b></td>
<td colspan="5"><input type='text' name="actname" size="130" required></td>
</tr>
<tr>
<td><b>Industry Type</b></td>
<td colspan="5"><input type="checkbox">
Select All Industries</td>
<?php
$conn = get_dbconnect();
$sql1 = "SELECT*FROM industry";
$result = mysqli_query($conn, $sql1);
$nrows = mysqli_num_rows($result);
//echo $nrows;
$sql = "SELECT name FROM industry"; //selection query
$rs = mysqli_query($conn, $sql); //odbc_exec($conn,$sql);
// if(mysqli_num_rows($rs)>0){
while ($nrows > 0) {
echo "<tr>";
for ($i = 0; $i < 5; $i++) {
$row = mysqli_fetch_assoc($rs);
echo "<td><input type='checkbox' name='industrytype[]' value='" . $row['name'] . "'/>" . $row['name'];
}
$nrows = $nrows - 5;
echo '</tr>';
}
?>
</tr>
<tr><td colspan="5"><input type="submit" name="submit" value="submit"></td></tr>
</table>
<input type="submit">
</form>
setupstates.php
<?php
if($_POST){
$chk = array();
$chk = $_POST['industrytype'];
$total = count($chk);
for($i=0; $i<$total; $i++){?>
<input type="checkbox" checked="checked" value="<?php echo $chk[$i] ?>"><?php echo $chk[$i]?>
<?php}}?>
此代码绝对有效:-
创建第一页- index.php
<div>
form action="process.php" method="post">
<label>Select your interest :</label>
<input type="checkbox" name="course[]" value="Java"> Java
<input type="checkbox" name="course[]" value="Php"> PHP
<input type="checkbox" name="course[]" value="Python"> Python
<input type="checkbox" name="course[]" value="JavaScript"> JavaScript
</div>
<div>
<button type="submit">Submit</button>
</div>
创建第二页- Process.php
<?php
$course = (isset($_POST['course'])) ? $_POST['course'] : array(); ?>
<p><strong>Courses :</strong>
<?php
if (count($course) > 0) {
foreach ($course as $course) {
echo $skill . ' ';
}
} else {
echo "No course has been selected";
}
?>
</p>
输出:-课程:Java PHP Python JavaScript
您正在传递多个复选框值,而不是一个。 这意味着您的 setupdstates.php 需要是一个循环来处理所有值。
请注意复选框很奇怪。 如果您不检查它,则不会向 PHP 发送任何内容。 所以你只会得到那些被检查的。
<?php //page1.php ?>
<form method="post" action="setupstates.php">
<table border=".3px" id='acttable'>
<tr>
<td><b>Act Name</b></td>
<td colspan="5"><input type='text' name="actname" size="130" required></td>
</tr>
<tr>
<td><b>Industry Type</b></td>
<td colspan="5"><input type="checkbox">Select All Industries</td>
<?php
$conn = get_dbconnect();
$sql1 = "SELECT * FROM industry";
$result = mysqli_query($conn, $sql1);
$nrows = mysqli_num_rows($result);
//echo $nrows;
$sql= "SELECT name FROM industry"; //selection query
$rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);
// if(mysqli_num_rows($rs)>0){
$n = 0;
while( $nrows > 0 ) {
echo "<tr>";
for($i=0; $i < 5; $i++) {
$row = mysqli_fetch_assoc($rs);
//
echo "<td><input type='checkbox' name='industrytype[]' value='".$row['name']."' onclick=checkall();/>".$row['name'] . "</td>";
}
$nrows = $nrows-5;
echo '</tr>';
}
?>
</tr>
</table>
<?php // setupstates.php ?>
<table>
<?php foreach ($_POST["industrytype"] as $value) : ?>
<tr>
<td><b>Industry Type</b></td>
<td>
<input type="checkbox" value="<?php print $value; ?>">
</td>
</tr>
<?php endforeach; ?>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.