Click to See Complete Forum and Search --> : Virgin


kylesite
10-20-2003, 01:19 AM
My first script. Please tell me if it would work or not.
<?php
session_start();
$user = $_SESSION['nam'];
echo "
<form action=\"$PHP_SELF\" method=\"post\">\n
<input type=\"text\" name=\"nam\" value=\"Name\">\n
<input type=\"submit\" value=\"Submit\">\n
<br><br>
";

if(empty($user)){
echo "<font color=\"red\">Please enter your name!</font>";
} else {
echo "<center><h1>Hello <font color=\"red\">$user</font></center></h1>";
}
?>Thankyou

LordShryku
10-20-2003, 01:26 AM
So first...if you want to know if it will work or not....test it.

So here's my two cents....
1) You don't seem to ever set the var $_SESSION['nam'], so that probably won't work.

2) Your coding assumes register_globals is on. You should learn to code assuming it is off.

3) Seperating the variable from the string is common practice.
eg.
echo "<form action=\"$PHP_SELF\" method=\"post\">\n";

// Should be

echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";

//Or

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";


HTH

kylesite
10-20-2003, 01:29 AM
So how would i set the variable $_SESSION['nam']?

LordShryku
10-20-2003, 01:32 AM
Well, try this

if(isset($_POST['nam'])) {
$_SESSION['nam'] = $_POST['nam'];
}

kylesite
10-20-2003, 01:35 AM
So then what would the whole PHP look like?

LordShryku
10-20-2003, 01:52 AM
No, that's not the whole thing. That just sets your session var if the form is submitted, and the name is filled in

kylesite
10-20-2003, 02:02 AM
Is this better?
<?php
session_start();
$user = $_SESSION['nam'];
echo "
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n
<input type=\"text\" name=\"nam\" value=\"Name\">\n
<input type=\"submit\" value=\"Submit\">\n
<br><br>
";

if(empty($user)){
echo "<font color=\"red\">Please enter your name above in the box and click \'Submit\'</font>";
} else {
echo "<center><h1>Hello <font color=\"red\">$user</font></center></h1>";
}
if(isset($_POST['nam'])) {
$_SESSION['nam'] = $_POST['nam'];
}
?>