Page 1 of 1

$_POST not working.

Posted: Mon Jan 24, 2011 3:59 pm
by gtr053
I am semi-new to PHP and I found myself having trouble during some form validation. For some reason $_POST is not working. I tried echoing some values from it. Nothing. Not even an error when there is nothing posted. I know echo is working because I can print simple strings. However, say for example, I have a field named "foo" and the data is posted to the same page for validation. When I don't even check whether the form was submitted or loaded for the first time, I can't even get an error message to work with. Any help would be much appreciated.

Code: Select all

<?php echo $_POST['foo']; ?>
It just does not work. :x

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:03 pm
by John Cartwright
Are you sure you are submitting your values through POST? Would you care to show the headers of the requests to verify this?

Secondly, you will get errors if you are using an uninitialized variable, however, your error reporting is either set to low, or display errors is disabled.

Development environment should always include

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);
What happens if you submit the button in the following script (just save this all in a single file).

Code: Select all

<?php 

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
   echo 'Post Request: <pre>'. print_r($_POST, true) .'</pre>';
}

?>

<form action="" method="POST"> 
   <input type="text" name="foo"> value="bar">
   <input type="submit"> 
</form>

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:16 pm
by gtr053
I can't even force an error. Like you said, I was trying to get an error for an uninitialized variable.
Due to security reasons, I'll have to obscure the code a bit (just names and fields but the functionality should be the same). I deleted a lot of stuff so I'm not sure if it's still valid XHTML but here it is stripped down. Granted I'm not echoing the posted data in the end, but this is a temporary means for debugging.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="/main.css" rel="stylesheet" type="text/css" media="all"/>
		<!--[if lt IE 7.]>
		<script defer type="text/javascript" src="/scripts/pngfix.js"></script>
		<![endif]-->
<title>Create an Account</title>
<script type="text/javascript" src="/scripts/validation.js"></script>
	</head>

	<body>
	<div id="container">
            <div id="header">
            	<div id="logo">
                    <a href="/"><img src="/images/logo.jpg" height="82" width="308" alt="CampusBoards" /></a>
                </div>
                <div id="menu">
                    <ul class="links">
                        <li><a href="#">Link1</a></li>
                        <li><a href="#">Link2l</a></li>
                    </ul>
                </div>
            </div>
			<div id="contents">
<h2>Create an Account</h2>
<noscript>
	<style type="text/css">
		#registration {
			display: none;
		}
	</style>
    <p>Please enable JavaScript.</p>
</noscript>
<?php echo $_POST['firstname']; ?>
<form id="registration" action="/register" method="POST">
	<input name="submitted" value="1" type="hidden" />
	<label for="firstname">Name:</label>
        <input name="firstname" id="firstname" value="First" type="text" title="First Name" onblur="fill(this)" onfocus="empty(this)" />
        <input name="middleinitial" id="middleinitial" type="text" value="M.I." size="2" maxlength="1" title="Middle Initial" onblur="fill(this)" onfocus="empty(this)" />
        <input name="lastname" id="lastname" type="text" value="Last" title="Last Name" onblur="fill(this)" onfocus="empty(this)" />
	<br />
    	<input name="" id="submit" type="submit" value="Submit" title="Submit" onclick="return validate(this)" />
</form>
<script type="text/javascript">
	regForm = document.getElementById("registration");
	regForm.reset();
	window.inputFields = regForm.getElementsByTagName("input");
	window.defaults = [];
	for (n = 0; n < window.inputFields.length; n++) {
		window.defaults[n] = window.inputFields.item(n).value;
	}
</script>
			</div>
			<div id="footer">

				<ul class="links">
					<li><a href="/about">About Us</a></li>
					<li><a href="/terms">Terms of Service</a></li>
					<li><a href="/privacy">Privacy Policy</a></li>
				</ul>
			</div>
		</div>
	</body>
</html>

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:22 pm
by John Cartwright
I've already explained why you are not receiving errors.

A common debugging technique is to eliminate the most likely causes first. Did you try the test case I gave you? What was the result?

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:23 pm
by gtr053
And when I tried that last block of code you posted, I got no output whatsoever. :banghead:

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:24 pm
by gtr053
I receive errors for invalid syntax and using require_once() to include files that do not exist on the server. I've never had this problem of displaying errors.

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:27 pm
by John Cartwright
gtr053 wrote:I receive errors for invalid syntax and using require_once() to include files that do not exist on the server. I've never had this problem of displaying errors.
Those are both fatal errors. How are you getting them both?

Regardless, that's probably why you are "suspecting" POST global is not working. If you tried my test in it's own file without any other code you'll likely find it works correctly.

As for error displaying, it's completely up to the server configuration... which can vary from installation to installation.

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:37 pm
by gtr053
John Cartwright wrote:Those are both fatal errors. How are you getting them both?

I don't get those errors now, but I would get them on the same server due to typos in previous development. The odd indentation you see in my HTML is caused by including other PHP files (for easy layout changes).
John Cartwright wrote:Regardless, that's probably why you are "suspecting" POST global is not working. If you tried my test in it's own file without any other code you'll likely find it works correctly.
Nope. :x
John Cartwright wrote:Development environment should always include error_reporting(E_ALL);
ini_set('display_errors', true);[/syntax]
I am not too sure what you meant by this.

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:41 pm
by John Cartwright
OK please enlighten me.

Explain exactly what happens when you post that test script in a separate and blank file, open it, and submit the button.
I am not too sure what you meant by this.
It means you should always develop with error_reporting(E_ALL) and display_errors = 1. It's best to set these in your php.ini since any settings made in code will not take affect upon a fatal error.

Re: $_POST not working.

Posted: Mon Jan 24, 2011 4:48 pm
by gtr053
I am simply getting no output. Nothing changes after submitting the form to itself. I added the two lines you recommended but I am seeing no difference.

Re: $_POST not working.

Posted: Tue Jan 25, 2011 3:01 pm
by gtr053
Okay, so I took out the if statement from that block of code and regardless of whether anything has been posted it or not, I get what appears to be an empty array. Correct me if I am wrong.

Code: Select all

array(0) { }
I have had this form work before and all I think I changed since the last time it worked was the layout. I had a table in the form rather than a bunch of div elements.

Re: $_POST not working.

Posted: Tue Jan 25, 2011 3:04 pm
by gtr053
Nevermind. I figured it out. I had an invalid value for the action attribute of my form element.

Re: $_POST not working.

Posted: Tue Jan 25, 2011 9:33 pm
by danwguy
gtr053 wrote:Nevermind. I figured it out. I had an invalid value for the action attribute of my form element.
No body saw that? you cant have action point to a directory, you need it to go to your processing php