Forms via PHP

SAbsar

Mac Graduate
Hi.
I'm just learning PHP, and I tried it out with forms, but it doesnt work. I mean the php script doesnt read the variables from the previous page!
for an example, if in the calling HTML page,

<form method='post' action='test.php'>
<input type='text' name='fname' value='Absar'>
<input type='submit' name='submit' value='Submit'>
</form>,

and in the test.php file,

<?php
print "Hello, ";
print $fname;
?>


it DOESNT print fname!!! Its printing "Hello, ", but not fname!
I cant figure out what im doing wrong! ive also tried GET instead of POST, but nothing works!

Help me out, will ya?
 
Newer versions of php ship with register_globals turned off and that's probably what you're dealing with. This means you need to access your variables like this:
$_POST['variablename']

So in your test.php file you would use:
print $_POST['fname'];

If you prefer using "friendlier" variable names while working with them, you can do this with your variables (in test.php):
$fname=$_POST['fname']

and then access them the regular way:
print "Hello, ";
print $fname;

For forms using get rather than post, you would use:
$_GET['fname']

For session variables:
$_SESSION['variablename']

It's possible to turn register_globals on, but for security reasons you're better off leaving it off and writing your scripts using the above method.
 
Back
Top