Page 1 of 1

Issues with POST between included pages - PHP noob

Posted: Thu Apr 22, 2010 5:56 pm
by KurtPW
Hello all:

I am an ASP/ASP.Net/C# desktop developer who is brand-spanking new to PHP. I have inherited an app. that a client had purchased and there seems to be some issue with one section.

The app uses a shell page (index.php) that dynamically includes other pages based on some params. One of the included pages has a form on it that *should* allow the user to delete some data, but absolutely nothing happens when submitted.

I debugged and found that a key variable was never being assigned any data so the code could never run. Next I found that the variable is not assigned because, it seems, the POST collection is empty even after a form submit.

The form, inside the included page, is pretty standard. It informs the index.php shell that it needs to include the keywords.php page:
< form action="index.php?q=keywords" method="POST" >

The shell page reads the querystring collection just fine and creates the correct include page as a result, but on the include page the POST always reads as empty. No variable set to a POST element ever has data. Is there any issue in sending a POST between an included and including page?

Thanks

Kurt

Re: Issues with POST between included pages - PHP noob

Posted: Thu Apr 22, 2010 6:12 pm
by Christopher
$_POST is a superglobal, so will be available in all PHP scripts. Can you show some code?

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 9:28 am
by KurtPW
Thanks for the reply, Christopher. Here is some code:

After you have first logged in, you click a link on the index.php page to load the keywords.php page. This information is passed in querystring to the index.php page itself, which acts as a shell for all other admin pages.

Code: Select all

if($q=="keywords") {
include("keywords.php");
exit;
}
The keywords.php page is loaded. It contains this form:
< form action="index.php?q=keywords" method="POST" >

The form inside keywords.php POSTs to index.php and once-again passes a querystring variable to instruct index.php to load keywords.php.
There are a few code blocks inside keywords.php that test for POST data. One example:
if (isset($_POST['newkeywords'])) {….

This should test whether or not new keywords have been entered into a textarea control. Even if data is entered into this control the ($_POST['newkeywords']) element is always empty. The same holds true for all other POST values tested, including this block:
elseif(isset($_POST['deletebutton']) && $_POST['deletebutton'] == 'Delete') { ….

The controls are named correctly and so are the values passed from buttons. I have also checked case and it is correct in the two examples above.
Being a PHP noob I decided to bypass all code on keywords.php and drop in a debug block to get down to the simple stuff:

Code: Select all

//============ DEBUG BLOCK =====================
if (isset($_POST["newkeywords"]) && $_POST["newkeywords"] != "") {
	echo "<font color='blue'>DEBUG keywords.php 19: </font><font color='red'>POST['newkeywords']) = TRUE</font><br/><br/>";
}
else {
	echo "<font color='blue'>DEBUG keywords.php 22:</font> <font color='red'>POST['newkeywords']) = FALSE</font><br/><br/>";
}

if (isset($_POST['deletebutton'])){
	echo "<font color='red'>deletebutton exists in POST</font><br/>";
}
else {
	echo "<font color='blue'>deletebutton NOT POSTed</font><br/>";
}
	
if($_POST['deletebutton'] == 'Delete') {
	echo "<font color='red'>\deletebutton = 'delete'</font><br/>"; 	
}
else {
	echo "<font color='blue'>\deletebutton = 'delete'</font><br/>";	
}
echo "keyords = ".$_POST['newkeywords']."<br/>";
//==============================================

On first load after login this is the debug output:
DEBUG keywords.php 22: POST['newkeywords']) = FALSE
deletebutton NOT POSTed
\deletebutton = 'delete'
keyords =

What is also interesting to me is that the third debug line (\deletebutton = 'delete') shows the button value though the button hasn’t been clicked. 

After entering new keywords the debug output is:
DEBUG keywords.php 22: POST['newkeywords']) = FALSE
deletebutton NOT POSTed
\deletebutton = 'delete'
keyords =

And after selecting keywords for deletion this is the output:
DEBUG keywords.php 22: POST['newkeywords']) = FALSE
deletebutton NOT POSTed
\deletebutton = 'delete'
keyords =

Suspecting that maybe PHP handled HTML form interaction and the POST collection differently from Microsoft products, I built a simple, single-page form that submits to itself and grabs POST data like so:
if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'] == 'xxx') {//
	echo "btnSubmit clicked<br/>";
}
else {
	echo "btnSubmit NOT clicked<br/>";
}
if(isset($_POST['addbutton']) && $_POST['addbutton'] == 'Add') {//
	echo "addbutton clicked<br/>";
}
else {
	echo "addbutton NOT clicked<br/>";
}
if(isset($_POST['updatebutton']) && $_POST['updatebutton'] == 'Update') {//
	echo "updatebutton clicked<br/>";
}
else {
	echo "updatebutton NOT clicked<br/>";
}
if ( isset($_POST['txt2']) && $_POST['txt2'] != ''){
	echo "txt2 is set<br/>";
}
else {
	echo "txt2 NOT set<br/>";
}
This works exactly as it should. Ugh.

Kurt

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 2:46 pm
by Christopher
Ahhhhh ... even though your form uses POST, you are sending that parameter on the command line (index.php?q=keywords) -- therefore using GET. You need to use $_GET['keywords'] to check for that parameter. It is little known fact that you can send GET parameters with a POST submission.

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 3:04 pm
by KurtPW
Christopher:

Thanks for the reply. That particular variable *is* being retrieved using $_GET, and it works. But the forms collection is where all the particular variables come from that are empty.

I have done some research and found a blog (wish I had saved the url) wherein a developer spoke of getting the same results. He found that there are two errors that can creep into the php.ini file that can cause this behavior without returning an error: 1) The MaxPostSize (or similar) value is set greater than the actual system memory or 2) The suffix behind the MB size of the POST is incorrect. In his instance 8 megs was listed as 8M. It should have been 8MB, and so was dumping the POST collection at the server.

I ran this code the code he suggested:
[contxt="php"]print "<br/>CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
$data = file_get_contents('php://input');
print "DATA: <pre>";
var_dump($data);
var_dump($_POST);
print "</pre>";[/context]

... and I could see POST values that I had entered so the raw POST seems to be formed correctly, which would lend some more credence to the idea that this may be a php.ini issue.

I cannot access php.ini through the online control panel so I have asked my client to make a call and get the contents of php.ini as it relates to POST size. maybe that will shed some light when I can view the file?

Thanks

Kurt

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 3:12 pm
by Christopher
Can you post the form HTML?

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 4:07 pm
by KurtPW
Christopher:

Here's the output HTML of the form. I have cut it way down as there are far too many select options and selects to paste at once:

<form action="index.php?q=keywords" method="POST">
<input type="hidden" name="token" value="7176273d497d40a061711c9889b0215f">
<table>
<tr>
<th>Delete</th>
<th>Keyword</th>
<th>Article</th>
</tr>
<tr>
<td align="center"><INPUT type="checkbox" name="delete[]" value="0"></td>
<td><input type="text" size=35 readonly name="keyword[]" value="xxx-yyy"></td>
<td>
<select name="article[]">
<option selected value="# random article #"># random article #
<option value="Belmont.txt">Belmont.txt
<option value="Facts.txt">Facts.txt
<option value="Pacific.txt">Pacific.txt
.... and many, many more
</select>
</td>
</tr>
<tr>
<td align="center"><INPUT type="checkbox" name="delete[]" value="1"></td>
<td><input type="text" size=35 readonly name="keyword[]" value="bssjsdjsd"></td>
<td>
<select name="article[]">
<option selected value="# random article #"># random article #
<option value="Belmont.txt">Belmont.txt
<option value="Facts.txt">Facts.txt
<option value="Pacific.txt">Pacific.txt
.... and many, many more
</select>
</td>
</tr>
<tr>
... the select repeats many times ....
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><input type="submit" name="deletebutton" value="Delete"></td>
<td colspan="2" align="center"><input type="submit" name="updatebutton" value="Update"></td>
</tr>
<th>&nbsp;</th>
<th>New Keywords</th>
</tr>
<tr>
<td>&nbsp;</td>
<td><textarea name="newkeywords" cols=35 rows=8></textarea></td>
<td width=200>
Please type or paste new keywords into the text area on the left (one per line). Use spaces or dashes to separate the words of keyword phrases.
Click the add button to write the new keyword files.
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="addbutton" value="Add"></td>
</tr>
</table>
</form>

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 4:50 pm
by Christopher
How big is your form data? I can't imagine that your are posing more that a megabyte of data in a form. That limit is usually for file uploading. Do any forms work? If you cut that form in half does it work? What have you tried?

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 4:57 pm
by KurtPW
I doubt it's a meg either. It's a combined total of about 160 fields.

1) I have built a new test form that posts to itself and that works fine. The POST collection is populated.

2) Then I tore out the HTML guts of that test page and placed them in an include to see what effect that might have. Nothing - it all worked great again.

3) Next I copied all code from the original index.php and keywords.php and dropped them on new pages suffixed with a 2. I commented out almost all code and just kept the 'newkeywords' textarea in keywords.php. Doing that it seems that POST data goes through just fine - I can see it on both forms.

This detective work makes me believe either the POST data is just too large (will do more testing) or that some other code is wiping out the POST after it is sent. I am going to slowly uncomment code and let the page build up a bit at a time to see what happens, debugging all the way. Tedious but hopefully productive.

Kurt

Re: Issues with POST between included pages - PHP noob

Posted: Fri Apr 23, 2010 6:37 pm
by Christopher
KurtPW wrote:I am going to slowly uncomment code and let the page build up a bit at a time to see what happens, debugging all the way. Tedious but hopefully productive.
Sound like the thing to do. You may want to check it in Mozilla or Chrome, if you have only tested it in IE.

Painful ... but that's programming sometimes ...