0

I bought the book "PHP 5/mySQL Programming for the Absolute Beginner."

So far I have been pleased with the book, but am stuck on a lesson and can't get past it, because the code given is either not correct(I did find an error in another lesson) or my webserver won't allow it.

I am sure there are many ways to write this particular code, but I just want the fix for this one as each lesson builds on the next. If someone could help me out that would be great! This lesson is about getting values from checkboxes using an if statement. (Please don't tell me about functions & arrays as that will be in upcoming lessons.)

This is the HTML page called

checkDemo.html:
<html>
<head>
<title>CHECK DEMO</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<h1>Check Demo </h1>
<h3>Demonstrates checkboxes</h3>
<h3>What would you like with your order?<br>
</h3>
<form action = "checkdemo.php">
  <ul>
    <li> 
      <p> 
        <input type="checkbox" name="chkFries" value="1.00">
        Fries</p>
    </li>
    <li> 
      <p> 
        <input type="checkbox" name="chkSoda" value=".85">
        Soda</p>
    </li>
    <p>
    <li> 
      <input type="checkbox" name="chkShake" value="1.30">
      Shake</li>
    <p>
    <li> 
      <input type="checkbox" name="chkKetchup" value=".05">
      Ketchup</li>
  </ul>
<p>
  <input type="submit">
</form>
</body>
</html>
----------------------------------------------------------------------------
This is the PHP page called checkDemo.php:

<html>
<head>
<title>CHECK DEMO</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<h1>Check Demo </h1>
<h3>Demonstrates reading checkboxes</h3>

<?


print <<<HERE

chkFries : $chkFries <br>
chkSoda : $chkSoda <br>
chkShake : $chkShake <br>
chkKetchup : $chkKetchup <br>
<hr>

HERE;

$total = 0;

if (!empty($chkFries)){
	print ("You chose Fries<br> \n");
	$total = $total + $chkFries;
} // end if

if (!empty($chkSoda)){
	print ("You chose Soda<br> \n");
	$total = $total + $chkSoda;
} // end if

if (!empty($chkShake)){
	print ("You chose Shake<br> \n");
	$total = $total + $chkShake;
} // end if

if (!empty($chkKetchup)){
	print ("You chose Ketchup<br> \n");
	$total = $total + $chkKetchup;
} // end if

print "The total cost is \$$total \n";

?>





</body>
</html>

For some reason it will print the checkbox names, but no values.

Please help! Thanks!

6
Contributors
10
Replies
15
Views
6 Years
Discussion Span
Last Post by shoucate
0

The book you are using may be old. It relies on a very old, deprecated method called 'register globals.'

With register globals, form input field names were automagically converted into global variables of the same name. That technique turned out to be a real security headache though and is not used in newer versions of php (at least, it is turned off by default in the configuration file.)

Querystring entries are stored in a 'super-global'called $_GET and POST form entries are stored in $_POST.

Change lines ~56 to read like this:

chkFries : {$_GET['chkFries']} <br>
chkSoda : {$_GET['chkSoda']} <br>
chkShake : {$_GET['chkShake']} <br>
chkKetchup : {$_GET['chkKetchup']} <br>
0

Thank you madCoder! I just tried that and it now prints the value of the boxes I checked. How do I fix the part where it prints out my order and totals the values?

0

Oh, same problem. Easiest fix is to map the $_GET entries into variables yourself:

$chkFries = floatval ($_GET['chkFries']);
$chkSoda = floatval ($_GET['chkSoda']);
$chkShake = floatval ($_GET['chkShake']);
$chkKetchup = floatval ($_GET['chkKetchup']);

I use the floatval() function to make sure that you got a floating point number. Not strictly necessary for this example though.
NEVER TRUST USER INPUT. Forms can be easily hacked.

0

Instead of using what madCoder has said use this. What the book has left out is the actual way of reading the checkboxes which is ironical.

Add the following code in just after the <? in your Checkdemo.php file that would be at line 53.

$chkFries=$_GET['chkFries'];
$chkSoda=$_GET['chkSoda'];
$chkShake=$_GET['chkShake'];
$chkKetchup=$_GET['chkKetchup'];

This would put the values into the variables you are using. $_GET is used to read the values from the HTML form into the variables. Like madcoder has said, this is called a super-global. To make sure this happens always tell the HTML file to store the code in the $_GET or $_POST by giving the method="get" or method="post" in your form.

<form action = "checkdemo.php" method="get">

All the best. Let us know if you need anything else.


Edit:

Oops! Madcoder and me both posted close together.... Sorry :-). He's right, this time round....

Edited by sudeepjd: n/a

0

Oh, same problem. Easiest fix is to map the $_GET entries into variables yourself:

$chkFries = floatval ($_GET['chkFries']);
$chkSoda = floatval ($_GET['chkSoda']);
$chkShake = floatval ($_GET['chkShake']);
$chkKetchup = floatval ($_GET['chkKetchup']);

I use the floatval() function to make sure that you got a floating point number. Not strictly necessary for this example though.
NEVER TRUST USER INPUT. Forms can be easily hacked.

I don't want to sound dim....but exactly where should I put that?

-1

Next time you have to buy the latest and greatest book for PHP/MySQL since from time to time books are advancing and innovating and we need to adapt the fast paced changing technology

0

Oh, same problem. Easiest fix is to map the $_GET entries into variables yourself:

$chkFries = floatval ($_GET['chkFries']);
$chkSoda = floatval ($_GET['chkSoda']);
$chkShake = floatval ($_GET['chkShake']);
$chkKetchup = floatval ($_GET['chkKetchup']);

I use the floatval() function to make sure that you got a floating point number. Not strictly necessary for this example though.
NEVER TRUST USER INPUT. Forms can be easily hacked.

I added this at the top after <? alomg with :

chkFries : $_GET['chkFries'];
$chkSoda : $_GET['chkSoda'];
$chkShake : $_GET['chkShake'];
$chkKetchup : $_GET['chkKetchup'];

and now everything prints out perfectly. Thanks Madcoder!

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.