0

I've taken the following code and placed them in their respective files. I've opened up register.html in Firefox and after entering the required details to the form it loads welcome.php albeit with out the entered information. The loaded page simply reads: "Welcome! You are years old!"

Both files are stored in the same folder.

Any suggestions on what I'm doing wrong?

register.html

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

welcome.php

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

</body>
</html> 
4
  • Where is the XML Part of your question? Commented Feb 21, 2012 at 22:57
  • Apologies, HTML. Will edit now. Commented Feb 21, 2012 at 23:08
  • 3
    Look into the source of the page 'generated' by welcome.php. Do you see php code in it? If yes - your server does not run php engine for .php files. Or are you trying to do it without HTTP server? Commented Feb 21, 2012 at 23:26
  • You're going to want to read up on cross-site scripting vulnerabilities. Your code demonstrates one by echoing unfiltered, user-supplied data. You'll want to learn how to mitigate this problem as you learn PHP. Commented Feb 22, 2012 at 5:54

4 Answers 4

2

For Apache:

1) Ensure the mime module is on.

2) Try making an .htaccess file in the same directory, with the following contents:

AddType application/x-httpd-php .php

This will only work if the rewrite module is on.

3) Otherwise, if you have access to the httpd.conf file, try adding that line somewhere in it, without the .htaccess file.

Make sure to restart the server any time you make changes to httpd.conf.

Edit:
I should have also mentioned to view source of the page, to see if the PHP code is in it. which would make these steps necessary.

Another troubleshooting technique is to add <?php error_reporting(E_ALL); ?> at the top of the page, in case PHP actually is running and since there are no errors. If it is disabled, it will enable reporting an Undefined index notice. It seems highly unlikely that it would be the case, but it also seems like a useful step that could help us understand exactly what's going on. Then again, I'm pretty sure PHP just isn't executing.

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

1 Comment

+1 PHP is probably not parsed (<? ... ?> becomes hidden in HTML).
1

Close your body tag.

<body>

not

<body

Comments

1

I used the code as you posted it exactly, even with the broken body tag, and it still worked.

You should take a look at your apache installation and check that it's working correctly.

I'd recommend you run a simple phpinfo test, which is basically a document (ending with .php) with only this in it:

<?php phpinfo(); ?>

Comments

-1

You can use this

<?php
if ($_POST) {
    extract($_POST);
        if (!empty($fname) || !empty($age)) {
            echo 'First Name: '.$fname.' Age: '. $age;
        } else {
            echo 'Enter first name and age';
        }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form</title>
</head>

<body>

<form action="" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

4 Comments

-1, extracting $_GET, $_POST, $_REQUEST is bad practice at the minimum and a security horror show at worst.
you really don't know... it is very useful when we are submitting very big form.... so you dont need to include everytime.. $_post hahahaha
Yes, but you're also discarding the most logical point for validating untrusted data.
i really don't know why we ignore using of extract, Charles, can you please explain in detail why we don't use extract, what is bad effect of this function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.