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

Click For Summary
The discussion centers on the use of HTML forms and PHP to handle form submissions. A user created a simple HTML form that sends data via POST to a PHP file, expecting to retrieve the values using variable names directly. However, the PHP code initially provided resulted in a blank page because it attempted to access the variables directly without using the correct method. The correct approach involves using the $_POST superglobal array to access the submitted form data, as demonstrated by the working code snippet that successfully echoes the values.Participants clarified the distinction between form fields and variables, emphasizing that form submissions send fields, which must be accessed through the $_POST array. Misinterpretations of textbook terminology were noted, with suggestions to seek updated resources for clearer explanations. Additionally, best practices for handling form data were discussed, including checking request methods and validating input to prevent errors and enhance security. The conversation highlights the importance of understanding PHP's handling of form data and the need for careful coding practices to ensure proper functionality.
nobahar
Messages
482
Reaction score
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
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"].
 
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.
 
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.
 
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.
 
Thanks voko, I have decided to get a different PHP book; a more recent one.
 
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
9K
Replies
1
Views
4K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
9K