Where should i put this code~ help~

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ultratoro
Forum Newbie
Posts: 2
Joined: Tue May 20, 2008 12:16 am

Where should i put this code~ help~

Post 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!!!
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Where should i put this code~ help~

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Where should i put this code~ help~

Post 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!
ultratoro
Forum Newbie
Posts: 2
Joined: Tue May 20, 2008 12:16 am

Re: Where should i put this code~ help~

Post 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:
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

Re: Where should i put this code~ help~

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Where should i put this code~ help~

Post 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.
Post Reply