Jump to content

[SOLVED] Most basic form question ever?


tHud

Recommended Posts

Hello,

 

I want to use this snippet to make sure the fields in a form are ok before processing it.

 

<FORM method="post">
To:<br>

<input type="text" name="name" size="40" value="<?php if($name) { print $name; } else { print "Insert Name"; } ?>">

<input name="action" type="submit" value="Update Form">

</FORM>

 

It works, but I don't understand why.

 

The if statement is checking for the variable $name, but I haven't used the code that I would normally use.

 

What I see in all my tutorials would lead me to use...

 

$name= $_POST['name];

 

So, I'm confused as to where the $name variable is being found.  :confused:

 

Thanks for your patience :)

Link to comment
https://forums.phpfreaks.com/topic/178324-solved-most-basic-form-question-ever/
Share on other sites

this works because as thorpe said

Seems your server is poorly configured and has register_globals switched on. This is never a good idea.

and therefore the if is checking whether the variable $name exists. As the variable has not been initialized/declared in the code snippet provided the result will be that the textbox will have "Insert Name" in it.

 

if you wanted to use this to display the name after submitting the form, then you are correct and you would need to add

 

$name= $_POST['name];

 

hope that makes sense :D

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.