php - HTML/Ajax/PHP - 如何检查是否/哪些复选框被选中,并为每个复选框执行不同的操作

标签 php html sql ajax

如上所述,我正在尝试创建一个简单的 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"/>&nbsp; &nbsp; &nbsp;
                Orders 
                <input type="checkbox" name="table" value="ord"/>&nbsp; &nbsp; &nbsp;
                Order Items 
                <input type="checkbox" name="table" value="itms"/>&nbsp; &nbsp; &nbsp;
                Books
                <input type="checkbox" name="table" value="book"/>&nbsp; &nbsp; &nbsp;
                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 更好:

Is "else if" faster than "switch() case"?

关于php - HTML/Ajax/PHP - 如何检查是否/哪些复选框被选中,并为每个复选框执行不同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449121/

相关文章:

php - 在 php facebook sdk 4.0.0 或更高版本中指定应用范围

php - 如何在不损失质量和大小的情况下对base64图像进行解码和编码

PHP 邮件未显示在 Gmail 中,但显示在 Hotmail 和其他第 3 方/ISP 帐户中

php - 在 apache 日志文件中找到代理雷达

html - 如何对齐文本框旁边的标签

html - 为什么图像 larrow 不是黄色背景?

html - 轮播指标不工作

sql - 如何识别引用特定表的所有存储过程

c# - 在 NHibernate 映射层中使用复杂的 where 子句

mysql - 选择 col1 为值的行和 col1 不是值的另一行