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

1. Jan 14, 2014

### nobahar

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 (Text):
<html>
<title>Likes form</title>
<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 (Text):

<?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 (Text):

<?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: May 6, 2017
2. Jan 15, 2014

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. Jan 15, 2014

### voko

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. Jan 15, 2014

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). 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. Jan 15, 2014 ### voko 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. Jan 21, 2014 ### nobahar Thanks voko, I have decided to get a different PHP book; a more recent one. 7. Jan 22, 2014 ### SixNein 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 (Text): 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 (Text):

if (isset($_POST['nickname'])) {$mynickname = $_POST['nickname']; } else { // do some error handling here. } So to wrap everything up.... Code (Text):$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: Jan 22, 2014