简体   繁体   English

外部功能中的条件不起作用

[英]Conditionals in external functions not working

This has been bugging me and I can't seem to figure out how to get around it. 这一直困扰着我,我似乎无法弄清楚如何解决它。 Say I have an index.php that includes another page dostuff.php that's just a big function. 假设我有一个index.php,其中包括另一个页面dostuff.php,这只是一个很大的功能。 On my index page I have a conditional that gets called by a button in a form: 在我的索引页面上,我有一个条件,该条件由表单中的按钮调用:

---index.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;

In my included function I have another form that calls another conditional within the function. 在我包含的函数中,我有另一种形式可以在函数中调用另一个条件。

---dostuff.php--- --- 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;
}

When I click the button on the index page the include is called and populates the page with its form. 当我单击索引页面上的按钮时,将调用include并将其表单填充到页面中。 However, when I click the button from the function, the form disappears and the conditional never executes. 但是,当我从函数中单击按钮时,该表单消失,并且条件语句永远不会执行。

Now, if I add the following to the index page: 现在,如果我将以下内容添加到索引页面:

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>";

Clearly the form button is being called as verified by the var_dump call but the conditonal never executes. 显然,已通过var_dump调用验证了表单按钮的调用,但条件永远不会执行。

CONTENT_TYPE: application/x-www-form-urlencoded
dostuff=dostuff
DATA:

string(15) "dostuff=dostuff"
int(15)
array(1) {
  ["dostuff"]=>
  string(7) "dostuff"
}

I searched here and just about everywhere else for an answer to this to no avail, so my question is why won't anything inside the conditionals work? 我在这里和几乎所有其他地方搜索了答案,但都无济于事,所以我的问题是,条件条件内的任何内容为什么不起作用? This seems very odd to me so I cobbled this together to test with. 这对我来说似乎很奇怪,所以我将其拼凑起来进行测试。

It's just pure logic - it'll be a lot clearer if you make your forms use method="GET" instead of POST. 这只是纯粹的逻辑-如果您使表单使用method="GET"而不是POST,它将变得更加清晰。

When you load it in the beginning you click on the button named "test" and so you have a $_POST['test'] value when you load the next time. 在开始时加载它时,单击名为“ test”的按钮,因此下次加载时具有$ _POST ['test']值。

Your code looks like this in index.php: 您的代码在index.php中如下所示:

if (isset($_POST['test'])) {
    test();
}

so of course test() is called and you see your HTML from dostuff.php. 所以当然调用了test(),然后您从dostuff.php中看到了HTML。

The button in dostuff.php is named "dostuff" so when you push that button the form is loaded again with a value in $_POST['dostuff'] ''BUT NO VALUE IN $_POST['test'] !!'' dostuff.php中的按钮被命名为“ dostuff”,因此当您按下该按钮时,表单将再次加载$ _POST ['dostuff']中的值“但$_POST['test']没有值!”

Once again your logic at the top of index.php is this: 再一次,您在index.php顶部的逻辑是:

if (isset($_POST['test'])) {
    test();
}

and since there is no value in $_POST['test'] your test() function never gets called and you never see the button you're expecting. 并且由于$ _POST ['test']中没有任何值,因此您的test()函数将永远不会被调用,并且您也永远不会看到期望的按钮。

Really - change your forms to GET (you'll have to use $_GET or $_REQUEST instead of $_POST) so you can see exactly what is set each time without having to resort to all the fancy file reading and I think it'll be really clear. 的确如此-将表单更改为GET (必须使用$ _GET或$ _REQUEST而不是$ _POST),这样您就可以准确地看到每次设置的内容,而不必诉诸于所有花哨的文件读取,我认为它将真的很清楚

The question hasn't changed, see below as what I have to show doesn't fit in comments. 问题没有改变,请参阅下文,因为我要显示的内容不适合评论。

If I'm in index.php and I view my source, I get this: 如果我在index.php中,并且查看了我的源代码,则会得到以下信息:

<form id="test" method="get" action="/test/index.php">
    <button name="test" form="test" type="submit" value="test">Test</button>
</form>

If I click test I now get: 如果单击测试,我现在得到:

<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>

If I use a condition to call my function I get nothing back. 如果我使用条件调用我的函数,我什么也得不到。 If I eliminate the condition on index.php, and just call test(); 如果我消除了index.php的条件,只需调用test();即可。 I get back: 我回来了:

<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>

Now if I click "Do Stuff!" 现在,如果我单击“做东西!” I get back: 我回来了:

<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>

So, the question remains the same. 因此,问题仍然相同。 Why doesn't this work? 为什么不起作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM