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!

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>

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?

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.

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....

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?

After line 52 where the script starts '<?'

No doubt books are the good source to learn any language like PHP etc.
But I suggest you as a friend you must consult some good site to learn PHP as a beginner.
http://www.php.net
http://www.phpmoot.com
etc.

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.