Help with $_POST undefined index on mozilla firefox

I’m trying the examples on Kevin Yank’s “Build Your Own Database Driven Web Site Using PHP & MySQL”.

In chapter 3, he talks about $_GET and $_POST. So far, everything has been fine with $_GET. However, I’m having issues with $_POST. I’ve written the code exactly as he did, but I get this error on the site:

Notice: Undefined index: firstname in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\welcome1.php on line 9

Notice: Undefined index: lastname in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\welcome1.php on line 10

However, on IE, I get no error.

Here is the code:

welcome5.html file…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Welcome5</title>
	</head>
	<body>
		<form action="welcome5.php" method="post">
			<div><label for="firstname">First name:
				<input type="text" name="firstname" id="firstname"/></label></div>
			<div><label for="lastname">Last name:
				<input type="text" name="lastname" id="lastname"/></label></div>
			<div><input type="submit" value="GO"/></div>
		</form>
	</body>
</html>

welcome5.php file…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Welcome5</title>
	</head>
	<body>
		<?php
		$firstname = $_POST['firstname'];
		$lastname = $_POST['lastname'];
		echo "Welcome to our website, " . 
			htmlspecialchars($firstname, ENT_QUOTES, "UTF-8") . " " . 
			htmlspecialchars($lastname, ENT_QUOTES, "UTF-8") . "!";
		?>
	</body>
</html>

Your errors may be set to E_STRICT, in which case, you have to check if the value is set first… e.g.;


$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : null;

Thanks for the quick response. Now, I don’t get an error message, but it’s still not “correct.” It doesn’t show the intended message.

For example, if I type “Kevin” as the first name and “Yanks” as the last name, it comes out correctly on IE. However, on Firefox, it just shows, “Welcome to our website, !” (no name is shown)

Also, I thought $_POST was supposed to hide the variables in the address bar, but it doesn’t do that either.

Browsers don’t control server error handling so for it to come up in Firefox but not IE is impossible, however i tested your code and it works perfectly fine in Firefox using the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Welcome5</title>
    </head>
    <body>
        <?php
        $firstname = (isset($_POST['firstname']) && !empty($_POST['firstname'])) ? $_POST['firstname'] : '';
        $lastname = (isset($_POST['lastname']) && !empty($_POST['lastname'])) ? $_POST['lastname'] : '';
        echo "Welcome to our website, " .
            htmlspecialchars($firstname, ENT_QUOTES, "UTF-8") . " " .
            htmlspecialchars($lastname, ENT_QUOTES, "UTF-8") . "!";
        ?>
    </body>
</html>

That’s the source of your problem… if you see the variables in your querystring, they’re GET params, not POST params. You could resolve your issues by swapping $_POST for $_REQUEST in your code above, but it’s probably better to figure out why your form is using a get method when you’re obviously specifying a post method.

FYI, also, the reason you were getting no error in IE is because by default it doesn’t display HTTP 500 (server error) messages… you have to uncheck the “show friendly error messages” option to see the underlying php error.

Here’s a thought… double-check the view-source on welcome5.html and make sure it’s method=“post”. My guess is you changed it from get to post and forgot to upload the latest version of your code.

I had the exact same problem with Firefox 3.6.17 . It worked in IE and Chrome but not firebox. I check Google log in (code) to see what I was doing wrong. It turns out that Firefox 3.6.17 is case sensitive in respect to POST in HTML. On my log in page I changed the case to lower and it solved the problem with everything working, very weird.:slight_smile:

Page 1

<html>
<FORM ACTION=“login.php” METHOD=“post”>
User Name: <input type = ‘text’ name=‘Users’><br>
<br>
Password: <input type = ‘password’ name=‘Password’><br>
<input type = ‘submit’ value =‘Log in’>
</FORM>
</html>

Page 2

html>
<?php
//note you have to use upper case here
$users = $_POST[‘Users’];
$password = $_POST[‘Password’];

if ($users&&$password)
{
$connect = mysql_connect(“localhost”,“root”,“”) or die (“couldnt connect”);
mysql_select_db(“signin”) or die (“couldnt find database”);
echo $users;
}
else
die (“Please enter a user name and password”);
?>

</html>