Jump to content

  • Log In with Google      Sign In   
  • Create Account


[web] PHP warnings...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 MatsK   Members   -  Reputation: 226

Like
0Likes
Like

Posted 16 March 2011 - 04:43 AM

Hi!
I'm new to PHP and trying to make a login form. I keep getting the warnings:

[b]Notice[/b]: Undefined index: TxtFname in [b]C:\Programfiler\xampp\htdocs\login.php[/b] on line [b]3[/b]

[b]Notice[/b]: Undefined index: TxtPwd in [b]C:\Programfiler\xampp\htdocs\login.php[/b] on line [b]4[/b]

Here is my code:

[size=2]<?php[/size]
[size=2]/* Created on: 16.03.2011 */[/size]
[size=2]$TxtFname = $_POST["TxtFname"];[/size]
[size=2]$TxtPwd = $_POST["TxtPwd"];[/size]
[size=2]
[/size]
[size=2]if (!isset($_POST["BtnSubmit"])) //If form is not submitted to itself yet...[/size]
[size=2]{ [/size]
[size=2]?>[/size]
[size=2]<html>[/size]
[size=2]<body>[/size]
[size=2]  <form method="post" action="<?php echo $PHP_SELF;?>">[/size]
[size=2]  Brukernavn:<input type="text" size="20" maxlength="20" name="TxtFname"><br />[/size]
[size=2]  Passord:<input type="text" size="20" maxlength="20" name="TxtPwd"><br /><br />[/size]
[size=2]  <input type="submit" value="Logg inn" name="BtnSubmit">[/size]
[size=2]  <br />[/size]
[size=2]  </form>[/size]
[size=2]  <?[/size]
[size=2]    } [/size]
[size=2]    else[/size]
[size=2]    {[/size]
[size=2]  	  //TODO: DB...[/size]
[size=2]    }[/size]
[size=2]  ?>[/size]
[size=2]</body>[/size]
[size=2]</html>[/size]



Sponsor:

#2 MatsK   Members   -  Reputation: 226

Like
0Likes
Like

Posted 16 March 2011 - 04:46 AM

Not sure why the code boxes get [size=2] and [ b ] in them.

I think the [ code ][/ code ] tags are bugged...

#3 benryves   GDNet+   -  Reputation: 1992

Like
1Likes
Like

Posted 16 March 2011 - 05:10 AM

The $_POST superglobal will only contain values of fields that have been POSTed to the page. To determine whether the values exist you can use isset:

if (isset($_POST['BtnSubmit'], $_POST['TxtFname'], $_POST['TxtPwd'])) {
    # BtnSubmit, TxtFname and TxtPwd have been submitted - try logging on.
}

Historically, register_globals would put any incoming variables (whether they come from $_SERVER, $_POST, $_GET etc) into their own variables (so you could access $_POST['TxtFname'] as $TextFname). This feature (which is a bit of a security hole) has been switched off by default for a while now, so $PHP_SELF should be $_SERVER['PHP_SELF'].
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

#4 MatsK   Members   -  Reputation: 226

Like
0Likes
Like

Posted 16 March 2011 - 05:19 AM

Hm.
I changed my code to:


<?php
/* Created on: 16.03.2011 */
$TxtFname = $_POST["TxtFname"];
$TxtPwd = $_POST["TxtPwd"];

if (!isset($_POST["BtnSubmit"], $_POST['TxtFname'], $_POST['TxtPwd'])) //If form is not submitted to itself yet...
{ 
?>
<html>
<img src="Boligmagasinet - headding.jpg">
<link rel=stylesheet href="login-style.css">

<body>
  <div class="hovermenu">
	<ul>
	  <li><a href="index.html">Hjem</a></li><a href="index.html"></a>
      <li><a href="about.html">Om oss</a></li>
      <li><a href="contact.html">Kontakt</a></li>
	  <li><a href="mags.html">Magasiner</a></li>
	</ul>
  </div>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Brukernavn:<input type="text" size="20" maxlength="20" name="TxtFname"><br />
  Passord:<input type="text" size="20" maxlength="20" name="TxtPwd"><br /><br />
  <input type="submit" value="Logg inn" name="BtnSubmit">
  <br />
  </form>
  <?
    } 
    else
    {
  	  //TODO: DB...
    }
  ?>
</body>
</html>


but I still get the warnings :\

#5 demonkoryu   Members   -  Reputation: 976

Like
0Likes
Like

Posted 16 March 2011 - 05:23 AM

You are always trying to read from $_POST["TxtFname"], $_POST["TxtPwd"] even when they aren't set. The code to read them should go into the "else" branch.

#6 MatsK   Members   -  Reputation: 226

Like
0Likes
Like

Posted 16 March 2011 - 06:12 AM

Really?

I've been looking at a tutorial.

Is the code there old code?

#7 SimonForsman   Crossbones+   -  Reputation: 5952

Like
2Likes
Like

Posted 16 March 2011 - 07:05 AM

Really?

I've been looking at a tutorial.

Is the code there old code?


Alot of php tutorials are just plain bad, (The same is true for language tutorials in general)
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#8 MatsK   Members   -  Reputation: 226

Like
0Likes
Like

Posted 16 March 2011 - 07:13 AM

Hm, yeah, I guess.
I ended up just changing the form so it POSTed to a regular, plain php file. I didn't really get the whole concept of POSTing to itself.

#9 leiavoia   Members   -  Reputation: 960

Like
0Likes
Like

Posted 18 March 2011 - 07:28 PM

When you grab variables from POST, REQUEST, or GET (or really, any array, but especially these ones), you need to check to see if the variable you re looking for was actually sent by the client. Get into the habit of doing this:

$foo = isset($_POST['foo']) ? $_POST['foo'] : NULL; // or some sane default

This avoids array index warnings. NEVER assume incoming data is what you would expect or that it is even present at all.

PS - and yes, 95%+ of PHP tutorials are awful.Everyone is an "expert"!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS