这一直困扰着我,我似乎不知道如何绕过它。假设我有一个index.php,其中包括另一个页面dostuff.php,这只是一个大函数。在我的索引页面上,我有一个条件,它被表单中的按钮调用:
---index.php--
<?php
include 'dostuff.php';
if (isset($_POST['test'])) {
test();
}
echo '<form id="test" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo '<button name="test" form="test" type="submit" value="test">Test</button>'.PHP_EOL;
echo '</form>'.PHP_EOL;
在我包含的函数中,我有另一个形式,它调用函数中的另一个条件。
---dostuff.php--
<?php
function test() {
if (isset($_POST['dostuff'])) {
echo '<h1>Testing Do Stuff.</h1>';
}
echo '<form id="dostuff" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo '<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>'.PHP_EOL;
echo '</form>'.PHP_EOL;
}
当我点击索引页面上的按钮时,include会被调用,并用它的表单填充页面。但是,当我从函数中点击按钮时,表单会消失,条件永远不会执行。
现在,如果我将以下内容添加到索引页面:
print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<br>";
$data = readfile('php://input');
$contents = file_get_contents('php://input');
print "<br>";
print "DATA: <pre>";
var_dump($contents);
var_dump($data);
var_dump($_POST);
print "</pre>";
很明显,表单按钮是在var_dump调用验证的情况下调用的,但conditional从未执行。
CONTENT_TYPE:应用程序/x-www-form-urlencodeddostuff数据:string(15)"dostuff=dostuff"int(15)阵列(1){["dostuff"]=>string(7)"dostuff"}
我在这里和其他地方寻找答案,但都没有结果,所以我的问题是,为什么条件句中的任何内容都不起作用?这对我来说似乎很奇怪,所以我把它拼凑在一起进行测试。
这只是纯粹的逻辑——如果您让表单使用method="GET"
而不是POST,它会更加清晰。
当你一开始加载它时,你点击名为"测试"的按钮,所以下次加载时你会有一个$_POST["测试"]值。
您的代码在index.php:中如下所示
if (isset($_POST['test'])) {
test();
}
因此,调用test(),您可以从dostuff.php.中看到HTML
dostuff.php中的按钮名为"dostuff",因此当您按下该按钮时,表单将再次加载$_POST['dostuff']'中的值,但$_POST['test']
中没有值!!'
再一次,你在index.php顶部的逻辑是:
if (isset($_POST['test'])) {
test();
}
由于$_POST['test']中没有值,因此test()
函数永远不会被调用,也永远不会看到您期望的按钮。
真的——把你的表单改为GET
(你必须使用$_GET或$_REQUEST而不是$_POST),这样你就可以准确地看到每次设置的内容,而不必求助于所有花哨的文件读取,我认为这会非常清楚。
问题没有改变,请参阅下面,因为我要展示的内容不适合评论。
如果我在index.php中查看我的源代码,我会得到以下信息:
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
如果我点击测试,我现在得到:
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
如果我使用一个条件来调用我的函数,我将一无所获。如果我消除了index.php上的条件,只调用test();我回来了:
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
现在,如果我点击"做事情!",我会返回:
<h1>Testing Do Stuff.</h1>
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
因此,问题依然存在。为什么不起作用?