Forum Moderators: coopster

Message Too Old, No Replies

Beginners question: using isset to check button status

         

tierasan

8:09 pm on Sep 21, 2007 (gmt 0)

10+ Year Member



Hey all,

I'm new here, and thought I'd ask the experts about this little blip I've been having. I'm trying to write a simple script in order to check whether the submit button has been pressed, and depending on that, display certain text in the header. For my php test script, I have:
<?
$username = $_POST['username'];

if (isset($_POST['submit'])) //tests if the button has been pressed
{
if($username == "correctname"){
print(" - Welcome back, $username!");
}
else if($username!= "correctname"){
print(" - You'd better try again, $username!");
}
}

else {
print(" - Welcome!");
}
?>

The problem is, when I put in that extra if command in, then it perpetually displays me else command of " - Welcome!" in the header, even when submit is pressed.

Any ideas?

Thanks!
-Cal

cameraman

1:29 am on Sep 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, tierasan!

Are you sure the button on your form has name="submit"?
It's case-sensitive, so if your button says name="Submit" then your if condition would always evaluate false.

Also, it would be better to put:
$username = $_POST['username'];

inside that first if (next line after your opening brace).

tierasan

4:00 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



Thanks so much! I changed the name of the form to submit as well, and I placed the $username = $_POST['username']; string definition into within the first if brackets and it works perfectly.