Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] PHP warnings...

Started by MatsK Mar 16, 2011 at 10:43 AM 7 replies 2.2k views
Original Post
MatsK
MatsK
Hi!
I'm new to PHP and trying to make a login form. I keep getting the warnings:

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

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


[font="Times New Roman"]Here is my code:[/font]
[font="Times New Roman"] [/font][font="'Times New Roman"]
[size=2]
[size=2]<?php
[size=2]/* Created on: 16.03.2011 */
[size=2]$TxtFname = $_POST["TxtFname"];
[size=2]$TxtPwd = $_POST["TxtPwd"];
[size=2]

[size=2]if (!isset($_POST["BtnSubmit"])) //If form is not submitted to itself yet...
[size=2]{
[size=2]?>
[size=2]<html>
[size=2]<body>
[size=2] <form method="post" action="<?php echo $PHP_SELF;?>">
[size=2] Brukernavn:<input type="text" size="20" maxlength="20" name="TxtFname">

[size=2] Passord:<input type="text" size="20" maxlength="20" name="TxtPwd">


[size=2] <input type="submit" value="Logg inn" name="BtnSubmit">
[size=2]

[size=2] </form>
[size=2] <?
[size=2] }
[size=2] else
[size=2] {
[size=2] //TODO: DB...
[size=2] }
[size=2] ?>
[size=2]</body>
[size=2]</html>

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

I think the [ code ][/ code ] tags are bugged...
benryves
benryves
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 +++]
MatsK
MatsK
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>

<ul>
<li>Hjem</li>
<li>Om oss</li>
<li>Kontakt</li>
<li>Magasiner</li>
</ul>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Brukernavn:<input type="text" size="20" maxlength="20" name="TxtFname">

Passord:<input type="text" size="20" maxlength="20" name="TxtPwd">


<input type="submit" value="Logg inn" name="BtnSubmit">


</form>
<?
}
else
{
//TODO: DB...
}
?>
</body>
</html>



but I still get the warnings :\
demonkoryu
demonkoryu
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.
MatsK
MatsK
Really?

I've been looking at a tutorial.

Is the code there old code?
SimonForsman
SimonForsman

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)
[size="1"]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!
MatsK
MatsK
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.
leiavoia
leiavoia
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"!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.