首页 > 解决方案 > 使用 PHP 提交具有不同提交按钮和表单的页面

问题描述

我一个人无法解决这个问题。我有 .html 文件,在正文部分,我正在使用 switch 导入一些 .php 文件。我正在显示表格,并使用 POST 表单在其上导航。现在我想添加到我的导航栏搜索输入,它也将通过 POST 发送数据。它位于头部标签上,当我按下按钮时,什么都没有发生。

索引.php

<html>
<head>
    <div class="leftNav">
        <a class="buttonNav" href="index.php?subpage=table&data=session&page=0">Session</a>
        <a class="buttonNav" href="index.php?subpage=table&data=all&page=0">All</a>
    </div>

    <div class="rightNav">
        <?php include 'navbars.php' ?>
    </div>

</head>

<body style="font-family:Verdana;">
    <div class="content">
        <?php
        if(isset($_GET['subpage']))
        {
            switch($_GET['subpage'])
            {
                case 'table':
                    include 'datatable.php';
                    break;
                default:
                    include 'home.php';
                    break;
            }
        }
        else include 'home.php';
        ?>
    </div>
</body>

</html>

数据表.php

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if($_POST['search'])
        {
            $filtr = $_POST['filtr'];
            $data = $_GET['data'];
            header("
Location: index.php?subpage=table&data=".$data."&page=0&search=".$filtr);
        }
        else{...}  
    }
       //DISPLAYING TABLE//

        echo("
        </table>
        <form class='nav' method='POST'>");

        $disabled = '';
        if($curr+1 == 1)
            $disabled = "disabled='disabled'";
        echo("<input type='submit' name='f' value='First page' ".$disabled.">
            <input type='submit' name='p' value='Previous page' ".$disabled."> ");

        $disabled = "";
        if($curr == $max)
            $disabled = "disabled='disabled'";
        echo("<input type='submit' name='n' value='Next page' ".$disabled.">
        <input type='submit' name='l' value='Last page' ".$disabled."></form>");
?>

导航栏.php

<?php

    if(isset($_GET['subpage']) && $_GET['subpage'] == 'table')
    {
        echo("
        <form method='POST'>
            <input type='text' name='filtr'>
            <input type='submit' name='search' value='Search'>
        </form>
        ");
    }

?>

标签: phphtml

解决方案


如另一个答案中所述,您将需要使用该action属性,但您需要传递以下内容$_GET['subpage']

action="<?php if(!empty($_GET['subpage'])) echo '?subpage='.htmlspecialchars($_GET['subpage'], ENT_QUOTES) ?>"

注意:我没有看到您实际上在脚本中发送$_GET['subpage']or的任何$_GET['data']地方,所以我不知道它是否被正确调用。


推荐阅读