Jump to content
Larry Ullman's Book Forums

Chapter 2 - Handling An Html Form


Recommended Posts

Hi everyone!

 

I just started going through Mr. Ullman's book and really like it so far, but here's an issue that I'm going through right now...

 

I'm getting numerous errors after trying to run handle_form.php script from Chapter 2:

 

Notice: Undefined index: name in C:\xampp\htdocs\handle_form.php on line 27

 

Notice: Undefined index: email in C:\xampp\htdocs\handle_form.php on line 29

 

Notice: Undefined index: comments in C:\xampp\htdocs\handle_form.php on line 31

 

I went through Google Search and found out that I can suppress warnings by adding error_reporting(E_ALL ^ E_NOTICE); on top script.

 

That, however, doesn't solve an issue - it just masks the problem.

 

Any help would be appreciated!

Link to comment
Share on other sites

What code are you running

 

Lets see...

 

form.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD

XHTML 1.0 Transitional//EN""

http://www.w3.org/TR/XHTML1/DTD/

xhtml1-transitional.dtd">

 

<html xmlns="http://wwww.w3.org/1999/

xhtml" xml"lang="en" lang="en">

<head>

 

<meta http-equiv="content-type"

content="text/html; charset=

iso-8859-1" />

 

<title>Simple HTML Form</title>

 

</head>

 

<body>

 

<!-- Script 2.1 - form.html -->

 

<form action="handle_form.php" method="post">

 

<fieldset><legend>Enter your information in the form below:</legend>

 

<p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40"/></p>

 

<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="60" /></p>

 

<p><b>Gender:</b> <input type="radio" name="gender" value="M" /> Male <input type="radio"

name="gendder" value="F" />Female</p>

 

<p><b>Age:</b>

 

<select name="age">

 

<option value="0-29">Under 30</option>

 

<option value="30-60">Between 30 and 60</option>

 

<option value="60+">Over 60</option>

 

</select></p>

 

<p><b>Comments:</b><textarea name="comments" rows="3" cols="40"></textarea></p>

 

</fieldset>

 

<div align="center"><input type="submit" name="submit" value="Submit My Information" /></div>

 

</form>

 

</body>

 

</html>

 

handle_form.php

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD

XHTML 1.0 Transitional//EN""

http://www.w3.org/TR/XHTML1/DTD/

xhtml1-transitional.dtd">

 

<html xmlns="http://wwww.w3.org/1999/

xhtml" xml"lang="en" lang="en">

<head>

 

<meta http-equiv="content-type"

content="text/html; charset=

iso-8859-1" />

 

<title>Simple HTML Form</title>

 

</head>

 

<body>

 

 

<?php # Script 2.2 - handle_form.php

 

 

 

// Create a shorthand for the form data:

 

$name = $_REQUEST['name'];

 

$email = $_REQUEST['email'];

 

$comments = $_REQUEST['comments'];

 

/* Not used:

 

$_REQUEST['age']

 

$_REQUEST['gender']

 

$_REQUEST['submit']

 

*/

 

// Print the submitted information:

 

echo "<p>Thank you, <b>$name></b>, for the

following comments:<br />

 

<tt>$comments</tt></p>

 

<p>We will reply to you at <i>$email</i>.</p>\n";

 

 

 

?>

 

 

</body>

 

</html>

Link to comment
Share on other sites

Are you actually submitting the HTML form to the the handle_form.php or just running handle_form.php straight away?

 

Tried it both ways - its the same problem.

 

I don't see actual values showing up - I'm getting only variables themselves.

Link to comment
Share on other sites

What version of PHP are you using? If it is less than 4.1 you can't use $_REQUEST as it was added in that version. This is why Larry insists people furnish the versions they are using when asking for help. It's vital information.

 

Even if your version is 4.1 or later, try changing all instances of $_REQUEST to $_POST. Does it work then?

Link to comment
Share on other sites

What version of PHP are you using? If it is less than 4.1 you can't use $_REQUEST as it was added in that version. This is why Larry insists people furnish the versions they are using when asking for help. It's vital information.

 

Even if your version is 4.1 or later, try changing all instances of $_REQUEST to $_POST. Does it work then?

 

I'm using version 5.3.5 and, unfortunately, changing it to $_POST didn't fix anything.

 

Thank you for advice, though! :-)

Link to comment
Share on other sites

Interesting. Your code works for me (there are a few minor issues, which I'll let you try to figure out when you have it working).

 

Let's see what you are getting, if anything, from $_REQUEST. In the handle_form.php file, add this just before your comment about creating shorthand:

 

echo "<pre>\n";
print_r ($_REQUEST);  // prints the content of $_REQUEST array
print_r ($_POST);  // prints the content of $_POST array
echo "</pre>\n";

That should tell us what is held in those arrays, and maybe we can figure out why they aren't being read by the handle_form.php script.

Link to comment
Share on other sites

Interesting. Your code works for me (there are a few minor issues, which I'll let you try to figure out when you have it working).

 

Let's see what you are getting, if anything, from $_REQUEST. In the handle_form.php file, add this just before your comment about creating shorthand:

 

echo "<pre>\n";
print_r ($_REQUEST);  // prints the content of $_REQUEST array
print_r ($_POST);  // prints the content of $_POST array
echo "</pre>\n";

That should tell us what is held in those arrays, and maybe we can figure out why they aren't being read by the handle_form.php script.

 

Now it says this at the top of the form:

 

 

Array

(

)

Array

(

)

Link to comment
Share on other sites

But I still see only variables, not values on the resulting php page.

 

Something seems wrong with your PHP, you should not be seeing variable names with the code you provided. It doesn't seem to recognize HTTP headers (no $_POST or $_REQUEST variables). Add this to handle_form.php, either above or below where you added the code that prints the contents of $_REQUEST and $_POST:

 

echo php_sapi_name () . "<br />";

What does that produce?

Link to comment
Share on other sites

  • 2 years later...

well, i had exactly the same problem. but my issue was that i was using the same code (form.htm + handle_form.php) in same file and it was a php file. so going through this post, what i did is saved a bew file called form.html (containing the code for form) and another called handle_form.php containing the php code to get info from the form.html, process it and print it. ALL WORKED FINE.

thought i post it here for some novice like me to see :')

  • Upvote 1
Link to comment
Share on other sites

  • 2 years later...

what is diferrent between these 2 ?

 

look at  curly braces.

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
 
<body>
<form method="post" action="cal.php">
<fieldset><legend>The Cost</legend>
<table>
<tr><td>quantity</td><td><input type="text" name="quantity"  /></td></tr>
<tr><td>price</td><td><input type="text" name="price" /></td></tr>
<tr><td>Tax</td><td><input type="text" name="tax" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" /></td></tr>
</table>
<input type="hidden" name="submitted" value="1" />
 
</fieldset>
 
 
 
</form>
<?php
if(isset($_POST['submitted'])){
if (is_numeric($_POST['quantity']) and is_numeric($_POST['price']) and is_numeric($_POST['tax']))
$total=($_POST['quantity']*$_POST['price']);
$taxrate=( $_POST['tax'] / 100 );
$tax2=($taxrate*$total);
$end=($tax2+$total);echo $end;}
 
else
echo "!! fill it again";
 
 
 
?>
</body>
</html>
 
 
 
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
 
<body>
<form method="post" action="cal.php">
<fieldset><legend>The Cost</legend>
<table>
<tr><td>quantity</td><td><input type="text" name="quantity"  /></td></tr>
<tr><td>price</td><td><input type="text" name="price" /></td></tr>
<tr><td>Tax</td><td><input type="text" name="tax" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" /></td></tr>
</table>
<input type="hidden" name="submitted" value="1" />
 
</fieldset>
 
 
 
</form>
<?php
if(isset($_POST['submitted']))
if (is_numeric($_POST['quantity']) and is_numeric($_POST['price']) and is_numeric($_POST['tax'])){
$total=($_POST['quantity']*$_POST['price']);
$taxrate=( $_POST['tax'] / 100 );
$tax2=($taxrate*$total);
$end=($tax2+$total);echo $end;}
 
else
echo "!! fill it again";
 
 
 
?>
</body>
</html>
 
Link to comment
Share on other sites

The second one will give you a syntax error because you're missing the opening curly brace after if(isset($_POST['submitted'])). The first one will give you a syntax error because you're missing the opening curly brace after is_numeric($_POST['tax'])). They are both missing curly braces after the else, which won't cause an error but is very bad style.

Link to comment
Share on other sites

 Share

×
×
  • Create New...