Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

[web] PHP warnings...

4: Adsense

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   

376
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]



#2 MatsK   Members   

376
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   Members   

1999
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   

376
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   

980
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   

376
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   Members   

7633
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   

376
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   

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.