SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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...
    Code HTML4Strict:
    <!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...
    Code PHP:
    <!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>

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your errors may be set to E_STRICT, in which case, you have to check if the value is set first.. e.g.;

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

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by transio View Post
    Your errors may be set to E_STRICT, in which case, you have to check if the value is set first.. e.g.;

    PHP Code:
    $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.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    PHP 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($firstnameENT_QUOTES"UTF-8") . " " .
                
    htmlspecialchars($lastnameENT_QUOTES"UTF-8") . "!";
            
    ?>
        </body>
    </html>

  5. #5
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ttkim View Post
    Also, I thought $_POST was supposed to hide the variables in the address bar, but it doesn't do that either.
    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.

  6. #6
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Member
    Join Date
    May 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    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>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •