Page 1 of 1

[solved]PHP not receving POST variables

PostPosted: 19. February 2012 03:08
by GFSTCLAIR
I had this problem with 1.7.7, then decided to revert to 1.7.4, which I had installed some time ago but never actually used. Same symptoms.

Using Chrome, browse to localhost/readmusic.html :-

<html>
<head><title>Access the MUSIC Database</title></head>
<body onLoad="document.logon.user.focus();">
<form name="logon" action="readmusic.php" method="POST">
<p>Username: <input name="user" type="text"></p>
<p>Password: <input name="pswd" type="password"></p>
<p>Or just click <input type="submit" value="Go"> for anonymous access</p>
</form>
</body>
</html>

I just click Go, just to see what happens when readmusic.php is invoked:-

<?php

//Get the supplied name and password
if (!empty($_POST[user])) {
$user = $_POST[user];
} else {
// print "You forgot your name!"; // This check and the next made redundant by anonymous user feature.
// exit;
}
if (!empty($_POST[pswd])) {
$pswd = $_POST[pswd];
} else {
// print "You forgot your password!";
// exit;
}

// Open the connection
$conn = mysql_connect("localhost", $user, $pswd) or die(mysql_error());

Etc, Etc

I get this:-

Notice: Use of undefined constant user - assumed 'user' in C:\xampp\htdocs\readmusic.php on line 4

Notice: Use of undefined constant pswd - assumed 'pswd' in C:\xampp\htdocs\readmusic.php on line 10

Notice: Undefined variable: user in C:\xampp\htdocs\readmusic.php on line 18

Notice: Undefined variable: pswd in C:\xampp\htdocs\readmusic.php on line 18

I assume this has to be something absolutely elementary that I have missed, in the general area of telling the browser that a PHP interpreter is available "somewhere". The same code works OK on a different machine where I installed Apache and PHP separately, and had to modify a few initialisation files.

Re: PHP not receving POST variables

PostPosted: 19. February 2012 11:16
by Altrea
Hi GFSTCLAIR,

your posted code doesn't fit to the output i get on my XAMPP 1.7.7 development machine.

If i use your code, my error messages are
Code: Select all
Notice: Use of undefined constant user - assumed 'user' in C:\xampp177\htdocs\form_post\readmusic.php on line 4

Notice: Use of undefined constant user - assumed 'user' in C:\xampp177\htdocs\form_post\readmusic.php on line 5

Notice: Use of undefined constant pswd - assumed 'pswd' in C:\xampp177\htdocs\form_post\readmusic.php on line 10

Notice: Use of undefined constant pswd - assumed 'pswd' in C:\xampp177\htdocs\form_post\readmusic.php on line 11


What you can see is, that the two if clauses in lines 4 and 10 will return true, otherwise i wouldn't get the two messages of lines 5 and 11. Thats not the case in your own test. You have to find out, why your if-clauses will return false.

You can simply print_r the whole _POST-array to see all the keys and values:
Code: Select all
echo '<pre>';
print_r($_POST);
echo '</pre>';


To get rid of the "undefined constant" messages, simply put your array keys in single-quotes:
Code: Select all
//false
$user = $_POST[user];

//correct
$user = $_POST['user'];


best wishes,
Altrea

Re: PHP not receving POST variables

PostPosted: 19. February 2012 19:44
by GFSTCLAIR
I did get the same error msgs as you did when using IE8 - the Chrome ones were just a bit simpler! Your suggestion of quotes was exactly correct - in fact I found they were missing on another usage 30 lines further on (not posted). Also, to make the connect work with the anonymous user, I had to initialise $user & $pswd to the empty string - in your implementation an uninitialised string (naughty anyway!) doesn't suffice.

This one is solved, thank you, Graeme in PA.

Re: PHP not receving POST variables

PostPosted: 19. February 2012 21:11
by Altrea
GFSTCLAIR wrote:I did get the same error msgs as you did when using IE8 - the Chrome ones were just a bit simpler!

I use Chrome too as my primary browser, so i can't confirm that.
The messages are generated by the php interpreter. The browser doesn't have much influence on that.
Maybe it was a cache issue then.

GFSTCLAIR wrote:This one is solved, thank you, Graeme in PA.

Glad to hear that. I will mark this topic as solved.

best wishes,
Altrea