fishspy fishspy - 16 days ago 5x
PHP Question

PHP $_POST doesn't display data - Example code shown

I am new to PHP, I tried to work w3 schools example of posting data on forms..

It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!

Example #1: A simple contact from - HTML code

<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>

Example #2: Printing data from our form

Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

Expected output of this script may be:

Hi Joe. You are 22 years old.

Actual Output:

Hi . You are years old

The Post parameter is not displaying data.. Any help is really appreciated.


What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.

However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)

If you wish to make use of htmlspecialchars, do the following in your welcome.php file:

$fname = htmlspecialchars($fname);

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.

Form used:


<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">


I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)

Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname ); And placed within <?php and ?> tags such as:

$fname = htmlspecialchars( $fname );

NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.

Please visit this answer

StackOverflow did not let me insert the codes on that page, for one reason or another.