HTML and PHP issue: display HTML form variables using PHP echo

  • PHP
  • Thread starter nobahar
  • Start date
  • #1
493
2
Heeeeeeellllllllllllllooooooooooooo,

I have a question about HTML and PHP:

Firstly, I am using Apache2 and PHP5 (I think) and running it as a localhost.

I created a simple HTML form that sends the variables via post to a PHP file; there are two variables with the names "nickname" and "fullname", respectively:
e.g.
Code:
<html>
<head>
	<title>Likes form</title>
</head>
<body>
	<form action="[PLAIN]http://localhost/phptwo.php"[/PLAIN] [Broken] method="post">
			<h2>Info: </h2>
			<p>Nickname: </p>
			<input type="text" name="nickname" size="30" maxlength="30" />
			<br />
			<p>Fullname: </p>
			<input type="text" name="fullname" size="30" maxlength="30" />
			<br />
			<input type="submit" name="submit_likesform" value="Submit info" />
	</form>
</body>

</html>
According to a textbook, the PHP file should look like the following:
Code:
<?php

echo "$nickname";
echo "$fullname";

?>
This just gives me a blank page.

If I do the PHP file in the following way, it works:
Code:
<?php

echo $_POST["nickname"];
echo $_POST["fullname"];

?>
It isn't presented in any fancy way, but I get the variables to echo to the screen. I have also passed variables to PHPinfo and it works that way.

I can't see the book being wrong, so I must be missing something obvious.

Any help appreciated.
 
Last edited by a moderator:

Answers and Replies

  • #2
191
3
I'm not super familiar with PHP, but I just checked w3schools and according to them, form field values are put into an associative array as key/value pairs and you are correct in your approach (i.e. retrieving the value with $_POST["nickname"])

In the case of a GET request, you would have another array like $_GET["my_var"].
 
  • #3
6,054
391
I created a simple HTML form that sends the variables via post
That does not send "variables". That sends fields. So it should not be surprising that the variables, when you output them, contain nothing. The fields must be accessed via the _POST object, just like you did.

If your book said that posting a form sends variables, then, unfortunately, it is not a very good book. If it said that you could pass data via forms, it is correct, but you misinterpreted the statement.
 
  • #4
493
2
I'm not super familiar with PHP, but I just checked w3schools and according to them, form field values are put into an associative array as key/value pairs and you are correct in your approach (i.e. retrieving the value with $_POST["nickname"])

In the case of a GET request, you would have another array like $_GET["my_var"].
Thanks for the link Adyssa; as you say, they use $_POST, the book has so far made no mention of it (I found it on a web forum).

That does not send "variables". That sends fields. So it should not be surprising that the variables, when you output them, contain nothing. The fields must be accessed via the _POST object, just like you did.

If your book said that posting a form sends variables, then, unfortunately, it is not a very good book. If it said that you could pass data via forms, it is correct, but you misinterpreted the statement.
Looking back through the book, it does say 'send data'; it also used the words 'field' and 'variables' but without actually explaining in any way what is happening: none of these terms are explained or how they operate, I read "field" simply in the - I guess - colloquial sense of somewhere to input data, without much after-thought.
Correct wording aside, is there anyway the first .PHP file would work?, a setting maybe?, I can't see it being plain wrong and making its way into a textbook.

Thanks Adyssa and voko for the replies.
 
  • #5
6,054
391
You need to understand what a variable is, and what a field is.

Please have a look here: http://www.php.net/manual/en/security.globals.php

Your book must have been written when that directive was on by default. Do not rely on that behavior and do not try to turn it back on, learn to deal with your input properly.
 
  • #6
493
2
Thanks voko, I have decided to get a different PHP book; a more recent one.
 
  • #7
SixNein
Gold Member
42
16
Heeeeeeellllllllllllllooooooooooooo,

According to a textbook, the PHP file should look like the following:
Code:
<?php

echo "$nickname";
echo "$fullname";

?>
This just gives me a blank page.

If I do the PHP file in the following way, it works:
Code:
<?php

echo $_POST["nickname"];
echo $_POST["fullname"];

?>
When you fill out your forum and hit submit, php parses the data your browser sends and stores the results into the $_POST variable. But you have to be careful, because php won't set things the browser doesn't send. Suppose, for example, your browser sends a get request instead of a post request. If so, $_POST["nickname"] wouldn't be set.

So first it helps to find out what method the browser used:
Code:
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
// browser sent a post request

}
In addition, even after you know that the browser made a post request, you can't automatically assume it sent you expected information. So test to see if every variable you want has been set.

Code:
if (isset($_POST['nickname']))
{
$mynickname = $_POST['nickname'];
} else {
// do some error handling here.
}

So to wrap everything up....

Code:
$data = array (
   'nickname' => array('value'=>'', 'errors'=>''),
   'fullname' => array('value'=>'', 'errors'=>'')
)

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

   if (isset($_POST['nickname']))
   {
       $data['nickname']['value'] = $_POST['nickname'];
   } else {
       $data['nickname']['errors'] = "You didn't pass a nickname parameter.";
   }

   if (isset($_POST['fullname']))
   {
        if (strlen($_POST['fullname']) > 0)
        {
              $data['fullname']['value'] = $_POST['fullname'];
         } else {
              $data['fullname']['errors'] = "Full name is a required field!";
        }

   } else {
        $data['fullname']['errors'] = "You didn't pass a fullname parameter.";
   }

}
To print the passed fullname or nickname...

echo $data['nickname']['value'];
echo $data['fullname']['value'];

if we have any errors....

echo $data['nickname']['errors'];
echo $data['fullname']['errors'];

In a basic nutshell, programming for the web is a constant battle against hackers. Don't trust anything or anyone ever.
 
Last edited:

Related Threads on HTML and PHP issue: display HTML form variables using PHP echo

  • Last Post
Replies
23
Views
1K
Replies
4
Views
908
  • Last Post
Replies
15
Views
5K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
12
Views
731
Replies
3
Views
21K
  • Last Post
Replies
6
Views
5K
Replies
2
Views
9K
  • Last Post
Replies
4
Views
656
  • Last Post
Replies
4
Views
2K
Top