jessi
jessi

Reputation: 49

Reasons why $_POST would not echo html?

I'm new to PHP and trying to figure this out. I've been endlessly searching the internet for an answer to this, but nothing I've found, even on this site, has helped. There are a ton of similar questions posted, but none I've seen have answers that have solved my problem. The general issue is that my php file will not echo my html code.

I took this code straight from an online tutorial (it worked on that website) but when I try to run it from my computer, it doesn't work. The php code is called but the echo part is blank. I am running this from my AppServ www directory (Windows, if that makes any difference).

HTML file "Testing.html":

<html> 
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

PHP file "welcome.php":

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

Things I've tried:

  1. Saving both as .php files
  2. Using $_REQUEST instead of $_POST
  3. Adding ids as well as names to the input items in the html file
  4. Running from other directories

I'm out of ideas! Any suggestions?

Upvotes: 0

Views: 245

Answers (2)

jessi
jessi

Reputation: 49

Ahh, solved it. I put the port number into the url (eg localhost:8080) and all is well now. Thanks for the help everyone!

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

You need to first check weather $_POST is set or not before printing out on page

you can do this by using isset() function like below

isset($_POST["name"])

your code is vulnerable to XSS

You must sanitize any user input before it rendered back to the browser

just use htmlspecialchars(); for that

 htmlspecialchars($_POST["name"], ENT_QUOTES, 'UTF-8');

Good Read :

  1. XSS cheat sheet
  2. PHP the right way

Upvotes: 2

Related Questions