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

In summary, the book says that you need to set the variables in the PHP file, but if the browser sent a GET request instead, then the $_POST variables won't be set. The book also says that you can't automatically assume that the variables were sent. You need to test to see if the variables were sent.
  • #1
nobahar
497
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]  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:
Technology news on Phys.org
  • #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"].
 
  • #3
nobahar said:
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
Adyssa said:
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).

voko said:
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
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
Thanks voko, I have decided to get a different PHP book; a more recent one.
 
  • #7
nobahar said:
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:

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

What is HTML and PHP?

HTML (Hypertext Markup Language) is a markup language used for creating web pages. PHP (Hypertext Preprocessor) is a server-side scripting language used for creating dynamic web pages.

What is an HTML form variable?

An HTML form variable is a variable that is sent as part of an HTML form submission. It contains data entered by the user in a form field.

How can I display HTML form variables using PHP echo?

You can display HTML form variables using PHP echo by accessing the variable using the $_POST superglobal and then using the echo statement to output the value.

Can I use HTML and PHP together to create dynamic web pages?

Yes, HTML and PHP can be used together to create dynamic web pages. HTML is used for creating the structure and content of the page, while PHP is used for adding dynamic elements and functionality.

Do I need any special tools or software to work with HTML and PHP?

To work with HTML and PHP, you will need a text editor to write your code, a web server to run your PHP scripts, and a web browser to view your HTML pages. There are also many integrated development environments (IDEs) available that provide a more user-friendly way to work with HTML and PHP.

Similar threads

Replies
2
Views
1K
Replies
1
Views
2K
Replies
7
Views
5K
Replies
4
Views
6K
Replies
2
Views
9K
Replies
17
Views
5K
Replies
15
Views
6K
Replies
3
Views
9K
Back
Top