如上所述,我正在尝试创建一个简单的 html/PHP 页面。单击提交按钮时,我希望根据选中的复选框运行不同的 SQL 代码。SQL 代码非常简单,它只是为每个复选框显示不同的表格,但我不确定如何检查复选框是否被选中。我的 HTML 代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ricky Deacon, CSIS3380 Assignment 3. Show Table.html</title>
</head>
<body>
<h1>Book-O-Rama Catalog Search</h1>
<form action="showtab.php" method="post">
<fieldset><legend>Which Tables Would You Like To View?</legend>
<p>
Customers
<input type="checkbox" name="table" value="cust"/>
Orders
<input type="checkbox" name="table" value="ord"/>
Order Items
<input type="checkbox" name="table" value="itms"/>
Books
<input type="checkbox" name="table" value="book"/>
Book Reviews
<input type="checkbox" name="table" value="brev"/> <br /><br />
<input type="submit" name="submit" value="Show Tables">
</p>
</fieldset>
</form>
</body>
我还没有写 PHP 响应,因为我不确定从哪里开始
谢谢
最佳答案
以下是您需要执行的操作的更完整答案:
已编辑 - 还包括您输入的内容。
<input type="checkbox" name="table[]" value="cust"/>Orders
<input type="checkbox" name="table[]" value="ord"/>Order Items
<input type="checkbox" name="table[]" value="itms"/>Books
<input type="checkbox" name="table[]" value="book"/>Book Reviews
<input type="checkbox" name="table[]" value="brev"/>
<?php
// Check that the values you're trying to access have actually been posted
// 'table' is the 'name' of your input
if (!empty($_POST['table'])) {
// If it's not empty then set the variable
$tables = $_POST['table'];
}
// If it is empty (Your form didn't submit this input)
else {
// end processing or return to the previous page
return false;
}
// You will now need to loop through the array
foreach ($tables as $table) {
switch($table) {
case 'cust':
// Run Cust SQL Query
break;
case 'ord':
// Run ord SQL Query
break;
case 'itms':
// Run itms SQL Query
break;
case 'book':
// Run book SQL Query
break;
case 'brev':
// Run brev SQL Query
break;
}
}
?>
作为引用,为什么在这种情况下使用 switch
而不是 if/else
更好:
关于php - HTML/Ajax/PHP - 如何检查是否/哪些复选框被选中,并为每个复选框执行不同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449121/