Page 1 of 1

Problem with PHP and HTML Forms

PostPosted: 30. December 2006 19:49
by dgp0961
I am trying to get HTML forms working with xampp lite and having trouble getting them to work. I have included the HTML and PHP code below. As far as I can tell, I am coding this properly. The HTML code will display the form and when I select the Submit button on the form, it calls the PHP file but the PHP code is never executed. Any ideas?

HTML code - TestPHPForm.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<!--
HTML 3.2
Document type as defined on http://www.w3.org/TR/REC-html32
-->
<head>
<title>Title here!</title>
</head>
<body>

<h4>Tizag Art Supply Order Form</h4>
<form action="TestPHPForm.php" method="post">
<select name="item">
<option>Paint</option>
<option>Brushes</option>
<option>Erasers</option>
</select>
Quantity: <input name="quantity" type="text" />
<input type="submit" />
</form>


</body>
</html>

PHP code - TestPHPForm.php

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>TestPHPForm.php</title>
</head>
<body>
<?php

$quantity = $_POST['quantity'];
$item = $_POST['item'];

echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "Thank you for ordering from Tizag Art Supplies!";

?>
</body>
</html>

PostPosted: 30. December 2006 21:45
by Izzy
When a file contains a mix of html and php the file should be named with a php extension so the server knows to parse the php code.

So rename your file TestPHPForm.html to TestPHPForm.php and try and call it again from your browser for example:
http://localhost/TestPHPForm.php

You can then correct any code if required.

PostPosted: 30. December 2006 22:03
by dgp0961
Thanks for your reply. My HTML file only contains HTML code and has the extension *.HTML and my PHP file contains HTML and PHP code but has the extension *.PHP. So, I think I have things as you suggested. Just for grins, I renamed my *.html file to PHP and tried the code with the same result.

PostPosted: 30. December 2006 22:14
by Izzy
dgp0961 wrote:Thanks for your reply. My HTML file only contains HTML code and has the extension *.HTML and my PHP file contains HTML and PHP code but has the extension *.PHP. So, I think I have things as you suggested. Just for grins, I renamed my *.html file to PHP and tried the code with the same result.

You are indeed correct.
My mistake for not spotting you had 2 separate files.

I am not a php coder but maybe $_POST should be $_GET perhaps ????

Also can you not add some code at the beginning of your php that would return some error messages that might help find any issues?

If you don't get an answer in here you can post in English in the German PHP forum. There are many English speakers frequent that forum, martinpre, deepsurfer and Weidmann for example.
http://community.apachefriends.org/f/viewforum.php?f=6

PostPosted: 30. December 2006 23:08
by dgp0961
Thanks I will try that forum. I am fairly confident I have coded this correctly. I wonder if this is an issue with PHP and Forms in Windows or if I should place my HTML and PHP files in a specific directory so the PHP runtime can find them?

PostPosted: 13. September 2007 08:54
by DaWolf
I have tried your code and it all seems ok.
This is testHTMLform.html
Code: Select all
<!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 Document HTML</title>
</head>

<body>
<h1>Tizag Art Supply Order Form</h1>
<form action="testPHPform.php" method="post">
<select name="item">
<option>Paint</option>
<option>Brushes</option>
<option>Erasers</option>
</select>
Quantity: <input name="quantity" type="text" />
<input type="submit" />
</form>

</body>
</html>

and this testPHPform.php
Code: Select all
<!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>PHP Form Section</title>
</head>

<body>
<?php
$quantity=$_POST['quantity'];
$item=$_POST['item'];

echo "you ordered ".$quantity." ".$item.".<br />";
echo "Thank you for ordering from Tizag Art Supplies!";

?>
</body>
</html>

As you can see I made a couple of changes to the doctype and I called the files two different names.
Everything else is the same.
I also tried it the way you wrote it and it also worked, so your code is ok.