0

I am very new to PHP and HTML as well. I have written html code to read first name and last name as:

<!DOCTYPE html>
<html>
<body>
<form action="welcome.php" method="post">
First Name: <input type="text" name="Fname"><br>
Last Name: <input type="text" name="Lname"><br>
<input type="submit">
</form>
</body>
</html>

and I am calling welcome.php to insert some values into database as below.

<html>
<body>
Welcome <?php echo $_POST["Fname"]; ?>
<?php $db = pg_connect("host=localhost port= dbname= user= password="); ?>
<?php $query = "INSERT INTO employee VALUES ('sasi','kala')"; ?>
<?php $result = pg_query($query); ?>
?>
<a href="first.html"></a>
</html>
</body>

when I open html file by chrome browser, I am getting 2 fields as FirstName and LastName. After entering details and click on submit button, nothing is getting displayed or executed in php. If i remove html and body tag from php file, entire code is getting displayed on browser.How can i run this PHP script from HTML using my web browser. Any installation needed? Am i doing anything wrong. I am executing this code in windows web browser. Can anyone tell me the procedure?

kenorb
  • 155,785
  • 88
  • 678
  • 743
kala
  • 63
  • 2
  • 13

2 Answers2

0

You are closing </html> tag to early, you must need to follow this:

<html>
<body>
// your inner html
</body>
</html>

One more thing, you also have extra closing php tag ?> after this line, i hope this is just a typo:

<?php $result = pg_query($query); ?>

There are so many tutorials available on internet for you, one of: http://www.w3schools.com/html/

One more thing, if you want to use user input in your SQL Statement than note that, your query is open for SQL injection, you must need to prevent your query, this will help you: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Neither of this has any bearings on his problem, I'm afraid. Browsers are used to handling tag soup, so they fix it as necessary themselves. Though, that said, the HTML should indeed be cleaned up as you noted. – ChristianF Oct 20 '16 at 07:00
  • Oh, and I'm not sure I'd recommend the W3schools tutorials. They're unfortunately not that accurate, and directly wrong in some cases. – ChristianF Oct 20 '16 at 07:02
  • @ChristianF: but his form script file is working fine – devpro Oct 20 '16 at 07:03
  • Yes, because it's plain HTML. – ChristianF Oct 20 '16 at 07:03
  • @ChristianF: but he also mentioned `If i remove html and body tag from php file, entire code is getting displayed on browser.` – devpro Oct 20 '16 at 07:07
  • Yes. As I mentioned in my reply, doing that will render the file as plain text because of the .php extension. – ChristianF Oct 20 '16 at 07:13
  • @ChristianF: than bro you forgot this line `and i am calling welcome.php to insert some values into database as below.` – devpro Oct 20 '16 at 07:14
0

You need to install a web-server on your computer, and ensure that it includes the PHP parser.
This is because PHP is a server-side scripting language, as opposed to the client-side language JavaScript. So when you browser sees the PHP tags it doesn't know what they are, and thus promply ignores them (and their content, the actual PHP code). Which is also why you see the PHP code when you remove the HTML tag, as you're telling the browser that it's just plain text content at this point.

There are many popular packages for Windows when it comes to all-inclusive web-servers, WAMP being one of them.

PS: Ensure that you do not make this web-server accessible from the internet, as you will have all kinds of attacks performed against your computer then!

ChristianF
  • 2,068
  • 9
  • 14
  • I installed xampp and tried to open html file in browser still same result. I placed both html and php files on my desktop. Is there any specific place that i need to place php file? – kala Oct 20 '16 at 07:39
  • @kala Yes, you need to put the files in the web root folder. Once you've done that, you can use the following address to open them: http://localhost/form.html <- This assumes that the file containing the form is called "form.html". I recommend reading through the manual for XAMPP to learn how it works. – ChristianF Oct 20 '16 at 07:42