Page 1 of 1

Where should i put this code~ help~

Posted: Tue May 20, 2008 12:25 am
by ultratoro
I am just started to learn HTML+PHP... I am using the book called "PHP/MySQL Programming For The Absolute Beginner"

The codes is something like this...

Program 1:

<html>
<head>
<title>What's your name?</title>
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "post"
action = "hiUser.php">
Please type your name:
<input type = "text"
name = "userName"
value = "">
<br>
<input type = "submit">

</form>
</body>
</html>

Program 2:

<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<h3>PHP program that receives a value from "whatsName"</h3>

<?

print "<h3>Hi there, $userName!</h3>";

?>

</body>
</html>


I have tried to test run this code... for the program 1(.html) everything works fine.. but for the program 2(.php) it cant get the result from program 1...

There are some statement shown in the book..

IN THE REAL WORLD

Some PHP servers have turned off the ability to automatically create a variable from a form. You might be able to convince your server administrator to turn register_globals on in the PHP.INI file. If not, here's a workaround: If your form has a field called userName, add this code to the beginning of the program that needs the value of that field:

$userName = $_REQUEST["userName"];


* I have tried to turn on the register_globals or using $userName = $_REQUEST["userName"]; code but still same problem occur.. and i am not sure where to put this code... i guess its in the program 2 but where??

THANKS FOR THE ADVANCE!!!

Re: Where should i put this code~ help~

Posted: Tue May 20, 2008 12:43 am
by AXEmonster
there could be a couple of reasons for this

if PHP is installed and working properly then try this

Code: Select all

<?PHP
 
echo ""<h3>Hi there, $userName!</h3>";
 
?>
i cant see where your catching the form value

where is your $_POST variable.

Re: Where should i put this code~ help~

Posted: Tue May 20, 2008 12:45 am
by flying_circus
Hi Ultratoro,

Welcome! I will help explain what is going on here, but I first will make the suggestion that if your book is recommending that you use register_globals, that you find a newer book. It's much more common, almost entirely, to have register_globals turned off.

Ok, on to your question!

Program 1:

Code: Select all

<html>
  <head>
    <title>What's your name?</title>
  </head>
  <body>
    <h1>What's your name?</h1>
    <h3>Writing a form for user input</h3>
      <form method = "post" action = "hiUser.php">
      Please type your name: <input type = "text" name = "userName" value = "">
      <br>
      <input type = "submit">
    </form>
  </body>
</html>
Program 2: (named hiUser.php)

Code: Select all

<html>
  <head>
    <title>Hi User</title>
  </head>
  <body>
  <h1>Hi User</h1>
  <h3>PHP program that receives a value from "whatsName"</h3>
 
  <?php
    // We need to retrieve the value from the previous form
    $userName = $_POST["userName"];
 
    print "<h3>Hi there, $userName!</h3>";
  ?>
 
  </body>
</html>
As you can see, I just added 1 line of code to your second file. You need to define the variable $userName. All values from a form post can be retrieved using the $_POST["varName"] superglobal. Likewise, any variable from the querystring (example: http://www.mydomain.com/index.php?myVar=3) can be retrieved using the $_GET["myVar"] superglobal.

I hope this helps. If you have more questions, post them up!

Re: Where should i put this code~ help~

Posted: Tue May 20, 2008 10:14 am
by ultratoro
Thx to all! it works!

I added the code into my program 2 and it didn't work because of my own fault(maybe)....LOL... because i direct double click the .html file.... and i didn't know why can't double click. Is it IS A MUST to type the file location manually to the browser address?This is because I've tried this way and it works... blur @.@

Cannot double-click to access even it is .html file and link with .php? !! 8O :roll:

*Second time to post in forum... sorry for my English..... :lol: :banghead:

Re: Where should i put this code~ help~

Posted: Tue May 20, 2008 10:18 am
by timsewell
You can't just double click a file using PHP as it needs to be parsed by the PHP interpreter on the server - your localhost if you're running on a standalone computer.

Re: Where should i put this code~ help~

Posted: Tue May 20, 2008 1:06 pm
by califdon
I just want to emphasize what flying_circus said, you are learning from a very out-of-date book, and PHP has changed a lot in the past few years, including the use of global variables, for security reasons. There are many rather good tutorials available on the web that are more up-to-date. My favorite reference is http://w3schools.com, where you will find a wealth of reference information and tutorials on everything from HTML and CSS and Javascript to PHP, MySQL, Ajax, etc.