Page 1 of 1

PHP beginner help

Posted: Thu Jul 02, 2009 8:14 pm
by rozkan
Hello, i just started to learn PHP and using a book (6 years old) to learn it, there's some code examples and i've been coding according to these examples today i just wrote this code but it doesn't work. The code is exactly same as the example code but it just doesn't work, can somebody help me? Here's the code:

Code: Select all

<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php 
if (empty($userName)) { 
echo <<<HERE
<form>
Please enter your username:
    <input type = "text"
        name = "userName"> <br/>
    <input type = "submit"
        input value = "Enter">
</form>
HERE;
}
else {
echo "Hello $userName";
}
//end 
?>
</body>
</html>
Thanks!

Re: PHP beginner help

Posted: Thu Jul 02, 2009 8:21 pm
by DagasSiren
first off your not using method="POST" or action="$username" in your form declaration...

try looking at form declarations at: http://www.tizag.com/phpT/examples/formvar.php

hope this helps :) :)

Re: PHP beginner help

Posted: Thu Jul 02, 2009 11:28 pm
by califdon
In addition to the missing parameters for the <form ...> tag, you haven't retrieved the $username from the $_POST array. That's not an automatic assignment, you have to have code such as the following:

Code: Select all

if(isset($_POST['username'])) {
   $username=$_POST['username'];
}
There's more to it than that, namely, if you are going to do anything useful with the $_POST array data, like insert or update a database, it is very important to "sanitize" the data to protect your system from a hacker using SQL injection. But I don't want to confuse you at this point. Just remember that the above is only the first step.