Aaron
Aaron

Reputation: 321

POST/GET in PHP

I have a simple html form

 <!DOCTYPE html>
 <html>
 <form action="trial2.html" method="get">
 <input type="text" name="name">
 <input type="submit">
 </form>
 </html>

which then runs this trial2.html

<!DOCTYPE html>
<html>
<?php
$val = $_GET["name"];
echo($val);
?>
</html>

I cannot work out why echo won't print out "name" although it can print out generic strings

I've been using w3schools as a reference but I think I'm just being stupid somewhere/completely missing the point.

Upvotes: 0

Views: 66

Answers (3)

Sujeet Kumar
Sujeet Kumar

Reputation: 1320

We can't run PHP in .html files because our server does not run it unless we tell it to run. To do this we need to create a .htaccess file in your root web directory and add this line to it:

AddType application/x-httpd-php .htm .html

Otherwise simply change your file extention from .html to .php

Upvotes: 0

rybo111
rybo111

Reputation: 12588

A .html file does not normally contain PHP code.

Rename your file to the .php extension and adjust your code to point to the renamed file, and change the echo to:

echo $val;

Upvotes: 4

Kirk Powell
Kirk Powell

Reputation: 910

Most hosting companies don't enable embedded PHP in an html document. So, save the file as trial2.php and upload it to your server. The server will see the PHP extension and execute it. HTML code in a PHP file is rendered as HTML.

Upvotes: 0

Related Questions