2

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>
</form>

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.

10
  • 2
    and example 2 is from action.php? Commented Aug 14, 2013 at 17:29
  • try print_r($_POST) to see all the data in your _POST array if it's not there, you are doing something wrong Commented Aug 14, 2013 at 17:29
  • What is the name of your HTML file (with the HTML form)? If not action.php, then could you please post the contents of action.php? Suspect your problem is in there. Commented Aug 14, 2013 at 17:31
  • 2
    I would suggest, as an aside, that you find a better tutorial site. w3Schools has become somewhat notorious for out-of-date methods, as well as outright inaccuracies. It is important that you don't get started on the wrong foot. When you are ready to move into database access especially, find a more modern tutorial source! Commented Aug 14, 2013 at 17:36
  • 1
    Does something like <?php echo "hello"; ?> work? There may be a problem with your PHP installation and the page may be served unparsed. The browser would interpret <?php ?> as a tag and hide it. Check the source of the result page, just in case. Commented Aug 14, 2013 at 17:37

5 Answers 5

1

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:

<?php
$fname = htmlspecialchars($fname);
?>

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

Form used:

<html>
<body>

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

</body>
</html> 

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:

<?php
$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.

Sign up to request clarification or add additional context in comments.

22 Comments

Hello Fred, Even they don't do this as shown here.. but it works for them ! de.php.net/manual/en/tutorial.forms.php
@MonkeyZeus As so I was echoing it also, and it didn't work. The OP did not mention "how" he/she set it up. If the OP followed W3schools' example to a "T", it won't work.
@fishspy Hi. Um... have you by any chance "tested" it? MonkeyZeus also?
@Fred Good thing he isn't using W3 Schools
Hello Fred, I have saved thse 2 php files in my system , i don't get how to run it.. need to save in server or something..? I am using Xampp.. can yu pls explain on that too..
|
1

In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?

//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

.

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

EDIT

Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.

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

EDIT 2

Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.

//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.

12 Comments

It still doesn't work.. I am trying to work out an example as shown in de.php.net/manual/en/tutorial.forms.php ... This is the HTML file <form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well <p>Your name: <input type="text" name="name" /></p> <p>Your age: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form> and this is the PHP file //action.php Hi <?php echo htmlspecialchars($_POST['name']); ?>. You are <?php echo (int)$_POST['age']; ?> years old.
Hello MonkeyZeus.. I tried but it shows ; error_reporting(E_ALL); Hi . You are years old.
Hello MonkeyZeus, It doesn't show any error ! Output is Hi . You are years old. I still find that the data is not being displayed!!
Are you simply visiting to the action.php page or are you re-submitting the form each time?
I am re-submitting the form.. to give you more details.. I have PHP saved under "D:\Arun C\xampp\php" .. and this program is saved at "D:\RubyProgrammer\Projects\PHP" .... Each time I re-submit the form.. it goes to the PHP page, but no data is being displayed!
|
0
  1. 'post' or 'POST' both works fine in form tag.
  2. The following should be in action.php

     Hi <?php echo htmlspecialchars($_POST['name']); ?>.
    
     You are <?php echo (int)$_POST['age']; ?> years old.
    
  3. If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT

and check whats the error.

Most probably it should work now...

2 Comments

Hello Rahul, php.ini has this content -- error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
1. make error reporting as E_ALL & ~E_DEPRECATED & E_STRICT 2. just tell you are running it on wamp or xamp or any other server ?
0

In your form check if it is not sending empty values.

STEP 1 copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.

The following code should be in index.php

<form action="action.php" method="POST" onsubmit="return validate()">
    <p>Your name: <input type="text" name="name" id="name"/></p>
    <p>Your age: <input type="text" name="age" id="age"/></p>
    <p><input type="submit" /></p>
</form>

<script type="text/javascript">
    function validate(){
        var name=document.getElementById("name").value;
        var age=document.getElementById("age").value;
        alert ("Name="+name+" age="+age);
        return false;
    }
</script>

This code will check if the values are getting entered correctly without redirecting the page to action.php.

Step 2 If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)

    function validate(){
        var name=document.getElementById("name").value;
        var age=document.getElementById("age").value;
        if (name==null || name==""){
            return false;
        }
        if (age==null || age==""){
            return false;
        }
        return true;
    }

If both name and age are filled in the form, the submit will now redirect to action.php

Step 3 In action.php use the following code.

<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>

edited with instructions on OP's request

2 Comments

Hello dishwasher, Can you please tell me how to run the above code? Both the above Javascript functions come udner index.php?
include the first javascript and test the form. If you outputs are getting displayed correctly then replace it with the second one. And yes both goes in index.php. The first function will alert you with the output without redirecting the page and the second function is the actual validation of your form.
0

Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.