设置为 HTML 表单的操作时未执行 PHP 代码

Posted

技术标签:

【中文标题】设置为 HTML 表单的操作时未执行 PHP 代码【英文标题】:PHP code isn't being executed when set as the action for an HTML form 【发布时间】:2014-05-08 23:18:05 【问题描述】:

我正在创建一个包含 html 表单的网站,该表单具有一个空白操作,该操作指向表单所在的同一网页,以便可以使用 php 处理该表单。 PHP 代码没有被执行。你能看看我的代码并告诉我哪里出了问题吗?

<?php
if (isset($_POST['reportsubmit'])) 
    $radio = $_POST['customer'];
    if ($radio == 'customer') 
        $redirect = 'Click <a href="#customer">here</a> to continue on with the form';
        header('Location: #redirect');
     else if ($radio == 'item') 
        $redirect = 'Click <a href="#item">here</a> to continue on with the form';
        header('Location: #redirect');
     else if ($radio == 'department') 
        $redirect = 'Click <a href="#department">here</a> to continue on with the form';
        header('Location: #redirect');
     else if ($radio == 'person') 
        $redirect = 'Click <a href="#person">here</a> to continue on with the form';
        header('Location: #redirect');
    
    exit;
 else if (isset($_POST['customersubmit'])) 
    //process form
    //redirect
    exit;
 else if (isset($_POST['itemsubmit'])) 
    //process form
    //redirect
    exit;
 else if (isset($_POST['departmentsubmit'])) 
    //process form
    //redirect
    exit;
 else if (isset($_POST['personsubmit'])) 
    //process form
    //redirect
    exit;

?>

<!DOCTYPE html>
<html>
<head>
    <title>Gordmart MIS Reports</title>
    <!--<link rel="stylesheet" href="../css/Main.css">-->
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>
<body>
    <div data-role="page" class="frame" id="report">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a report grouped by customers, items sold, sales departments, or sales people?</h3>
            <form action="" method="post">
                <input type="radio" name="report" value="customer"><p>Customers</p>
                <input type="radio" name="report" value="item"><p>Items Sold</p>
                <input type="radio" name="report" value="department"><p>Sales Departments</p>
                <input type="radio" name="report" value="person"><p>Sales People</p>
                <input type="submit" name="reportsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="customer">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all customers, or a single report of just one?</h3>
            <form action="" method="post">
                <input type="radio" name="customer" value="all"><p>All</p>
                <input type="radio" name="customer" value="one"><p>One</p><br>
                <input type="submit" name="customersubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="item">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales items, or a single report of just one?</h3>
            <form action="" method="post">
                <input type="radio" name="item" value="all"><p>All</p>
                <input type="radio" name="item" value="one"><p>One</p><br>
                <input type="submit" name="itemsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="department">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales departments, or a single report of just one?</h3>
            <form action="" method="post">
                <input type="radio" name="department" value="all"><p>All</p>
                <input type="radio" name="department" value="one"><p>One</p><br>
                <input type="submit" name="departmentsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="person">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales people, or a single report of just one?</h3>
            <form action="" method="post">
                <input type="radio" name="person" value="all"><p>All</p>
                <input type="radio" name="person" value="one"><p>One</p><br>
                <input type="submit" name="personsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="redirect">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <?php echo $redirect;?>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
</body>
</html>

【问题讨论】:

$_POST['reportsubmit'] 是真的吗?另外,我很确定您无法重定向到锚点。您需要指定页面的实际 URL。 一旦你设置了header('Location: ...');,页面就不会被进一步处理,它会尝试重定向。 尝试print_r($_POST) 验证是否设置了$_POST['reportsubmit']; @leonardude 你喜欢用header('Location: #redirect')做什么? #redirect 是一个 html 锚点,不是要重定向的页面... 【参考方案1】:

尝试下一步:

1) 对于所有形式:

&lt;form action="" method="post"&gt; 替换为&lt;form action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?&gt;" method="post"&gt;

2) 对于所有重定向:

$findHttp = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
header('Location: '.$findHttp.'://'.$_SERVER["HTTP_HOST"].'/');
exit;

3) 检查 POST 请求 - var_dump($_POST); 并找到当前适用于该请求的“if”块。

【讨论】:

您应该使用htmlentities() 清理$_SERVER['self'] 回声。检查this SO question。【参考方案2】:

最佳做法是指定目标网址。如果你想自我总结试试这个

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']) ?>">

【讨论】:

您应该使用htmlentities() 清理该回声。检查this SO question。

以上是关于设置为 HTML 表单的操作时未执行 PHP 代码的主要内容,如果未能解决你的问题,请参考以下文章

将html表单中的数据插入sql数据库(html,php,sql)时未保存输入的值

在 iFrame jQuery 中重定向表单提交时未检测到更新的 Src 属性

Symfony 2 - 使用表单时未设置 Sluggable

html 博客:提交表单时未传递任何表单参数

复选框设置日期,但在表单加载时未选中:需要加载事件吗?

php表单传值问题