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. On my index page I have a conditional that gets called by a button in a form:
---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---
<?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. 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.
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.
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.
Your code looks like this in index.php:
if (isset($_POST['test'])) {
test();
}
so of course test() is called and you see your HTML from dostuff.php.
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']
!!''
Once again your logic at the top of index.php is this:
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.
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.
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:
<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(); 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?