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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
fishspy
  • 123
  • 1
  • 5
  • 14
  • 2
    and example 2 is from action.php? – twinlakes Aug 14 '13 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 – 19greg96 Aug 14 '13 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. – cssyphus Aug 14 '13 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! – Chris Baker Aug 14 '13 at 17:36
  • 1
    Does something like `` work? There may be a problem with your PHP installation and the page may be served unparsed. The browser would interpret `` as a tag and hide it. Check the source of the result page, just in case. – ironcito Aug 14 '13 at 17:37
  • @Chris I found W3Schools a very good tutorial site when I was a beginner. As long as you follow the HTML5 tutorials (not HTML4), you're fine. – FalconC Aug 14 '13 at 17:40
  • Hello ironcito - It works cool ! I am even able to connect to Mysql DB and pass data as a PHP file... But when it comes to $POST, where in it takes data from a HTML file .. it results empty! – fishspy Aug 14 '13 at 17:42
  • @FalconC Yeah, not bad for basic forms and HTML4 stuff. They are hopelessly out of date on HTML5, and their database access tutorials still advocate using the deprecated `mysql_*` functions, which is sure to poison a beginner's coding for the next year at best. I'm not a militant "NO W3SCHOOLS!!!11" guy, but it is worth mentioning that they are behind the times in some areas. A beginner has no tools to evaluate the quality of the tutorials they are exposed to. – Chris Baker Aug 14 '13 at 17:42
  • Make sure there is no redirection taking place. See [this question](http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission). – ironcito Aug 14 '13 at 17:52
  • What do you see when you right click and select view source? Do you see anything at all between `Hi` and `.`? – Sylwester Aug 14 '13 at 19:30

5 Answers5

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.
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • It still doesn't work.. I am trying to work out an example as shown in http://de.php.net/manual/en/tutorial.forms.php ... This is the HTML file
    // <-- make sure to capitalize method="POST" as well

    Your name:

    Your age:

    and this is the PHP file //action.php Hi . You are years old.
    – fishspy Aug 14 '13 at 17:38
  • Hello MonkeyZeus.. I tried but it shows ; error_reporting(E_ALL); Hi . You are years old. – fishspy Aug 14 '13 at 17:44
  • 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!! – fishspy Aug 14 '13 at 17:48
  • Are you simply visiting to the action.php page or are you re-submitting the form each time? – MonkeyZeus Aug 14 '13 at 17:49
  • 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! – fishspy Aug 14 '13 at 17:52
  • You are using XAMPP? In your web browser URL do you see http://localhost/index.html or do you see D:\RubyProgrammer\Projects\PHP ? – MonkeyZeus Aug 14 '13 at 18:00
  • That sounds correct too but I think your issue is beyond the scope of a simple programming error. You will have to look into the configuration of your XAMPP – MonkeyZeus Aug 14 '13 at 18:08
  • Do you have a real web server you can test these files on? Like a hosting plan with another company? – MonkeyZeus Aug 14 '13 at 18:32
  • no .. I am using Xampp now! But i will host it later elsewhere> I am using it for learning purpose.. – fishspy Aug 14 '13 at 18:39
  • Thats ok, try it without the htmlspecialchars() or the (int) like my EDIT 2 – MonkeyZeus Aug 14 '13 at 18:43
  • try var_dump($_POST); to see what you get try print_r($_POST); too. – vee Aug 14 '13 at 18:44
  • hello Monkeyzeus, I tried it .. this is what I get Hi . You are years old. Still data is not displayed! – fishspy Aug 14 '13 at 18:52
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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hello Fred, Even they don't do this as shown here.. but it works for them ! http://de.php.net/manual/en/tutorial.forms.php – fishspy Aug 14 '13 at 18:10
  • @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. – Funk Forty Niner Aug 14 '13 at 18:27
  • @fishspy Hi. Um... have you by any chance "tested" it? MonkeyZeus also? – Funk Forty Niner Aug 14 '13 at 18:29
  • @Fred Good thing he isn't using W3 Schools – MonkeyZeus Aug 14 '13 at 18:30
  • 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.. – fishspy Aug 14 '13 at 18:37
  • @MonkeyZeus Yeah.. I know. And I see what the OP is trying to do. The use of `htmlspecialchars` and need to be used in a different manner. I will edit my answer in a few minutes. – Funk Forty Niner Aug 14 '13 at 18:38
  • Hello Fred, I placed those 2 php files inside www flder of xampp directory say d\xampp\www\form.php and action.php.. when i host it in the browser say localhost\form.php it results in an error; Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16 – fishspy Aug 14 '13 at 18:44
  • @fishspy Yes, you have to run it from a server. However, I don't run my own server on my computer. – Funk Forty Niner Aug 14 '13 at 18:53
  • @fishspy I know very little about Webservers on own computer, but I'm pretty sure you need to call it from your Web browser like this `http://localhost/www/form.php` or using the location on your computer. Or try `\\localhost\c:\path\to\form.php` have a look at this on SO http://stackoverflow.com/a/312886/1415724 may help. – Funk Forty Niner Aug 14 '13 at 19:01
  • Hello Fred - I ran the script from the xampp folder & get this error in the browser Object not found! http://localhost/xampp/forms.php The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16 – fishspy Aug 15 '13 at 06:58
  • @fishspy I suggest you post a question about it. I really would like to help, but as I said, I know nothing about that subject, sorry :( – Funk Forty Niner Aug 15 '13 at 12:50
  • Hi Fred, It feels great that I got my first script running.. under xampp/htdocs .. i placed 2 php files.. and ran it as localhost/test.php in my browser.. looks good uh ! but here is form1...
    Name: Age:

    form2.php - is displaying a blank page; //These code goes in action.php extract ($_POST); echo "Hi $name. You are $age years old"; ?> i m close to finish things..

    – fishspy Aug 15 '13 at 13:22
  • @fishspy If you are using `dishwasher's` code, you will have to chat with him/her about it, from what I saw that you just typed in ` //These code goes in action.php extract ($_POST);...` those are his/her codes. Basically what needs to be done is this: 1) Call your form **form.php** then set as `action="welcome.php` to it. Your second file with the PHP needs to be called **welcome.php** – Funk Forty Niner Aug 15 '13 at 13:30
  • ya Fred i did that exactly ! but welcome.php shows only a blank page.. i m using a simple php example as shown here http://myphpform.com/php-form-tutorial.php ..I guess i m able to submit data and utl changes as localhost/welcome.php but yet that is blank, it is supposed to display data ! if this works for me ! my proj can kick-start successfully ! I have a long way to go in PHP.. – fishspy Aug 15 '13 at 13:32
  • @fishspy Try setting your action like this `
    ` see if that works. I tried that same tutorial and it worked for me. I am on a paid hosting using PHP 5.4, not my own webserver.
    – Funk Forty Niner Aug 15 '13 at 13:39
  • i am done fred... actually i had misplaced files inside the server.. now it works like a gem ! I had struggled for 48+ hours.. thanks for all your support ! I guess i c an start my game in php :) – fishspy Aug 15 '13 at 13:43
  • @fishspy Great, I'm happy to hear that it works for you :) Could you accept or upvote my answer? – Funk Forty Niner Aug 15 '13 at 13:47
  • @fishspy The least you could do is UPVOTE my answer, what with all the help I've given you. – Funk Forty Niner Aug 15 '13 at 14:28
  • Fred - I am not able to upvote.. But i marked your suggestions as my answer ! – fishspy Aug 15 '13 at 15:54
  • @fishspy Thank you :) I think you are able to upvote now, you have 13 rep points. – Funk Forty Niner Aug 15 '13 at 15:56
  • Vote up requires 15 Reputations :) – fishspy Aug 15 '13 at 16:04
  • It looks like you have 16, don't you? – Funk Forty Niner Aug 15 '13 at 16:09
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...

Rahul
  • 1,181
  • 1
  • 11
  • 20
  • Hello Rahul, php.ini has this content -- error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT – fishspy Aug 14 '13 at 17:46
  • 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 ? – Rahul Aug 14 '13 at 17:52
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

  • Hello dishwasher, Can you please tell me how to run the above code? Both the above Javascript functions come udner index.php? – fishspy Aug 14 '13 at 17:59
  • 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. – dishwasherWithProgrammingSkill Aug 14 '13 at 18:18
0

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