Stuck with simple PHP

Associate
Joined
2 Aug 2005
Posts
589
I'm in the early stages of learning PHP, and am very stuck as to why this code doesn't work.

each of the variables, chkSoda, chkFries,etc have a value associated with them, for some reason the $total variable isn't adding up those values.

Any help would be appreciated:

checkDemo.php
Code:
<html>
    <head>
        <title>Checkbox Demo</title>
    </head>
    
    <body>
        <h3>Demonstrates reading checkboxes</h3>
        
    <?
    
        $chkFries =     $_GET["chkFries"];
        $chkSoda =      $_GET["chkSoda"];
        $chkShake =     $_GET["chkShake"];
        $chkKetchup =   $_GET["chkKetchup"];
        
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>

checkDemo.html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Checkbox Demo</title>
</head>
<body>
    <h1>Checkbox Demo</h1>
    
    <h3>Demonstrates Checkboxes</h3>
    
    <form action = "checkDemo.php">
        
    <h3>What would you like with your order?</h3>
    
    <ul>
        <li><input type= "checkbox"
                   name= "chkFries"
                   value="1.00">Fries
        </li>
        <li><input type= "checkbox"
                   name= "chkSoda"
                   value=".85">Soda
        </li>
        <li><input type= "checkbox"
                   name= "chkShake"
                   value="1.30">Shake
        </li>
        <li><input type= "checkbox"
                   name= "chkKetchup"
                   value=".05">Ketchup
        </li>
    </ul>
    
    <input type="submit"
    
    </form>
      
</body>
</html>
 
Brains too tired to think but don't you need a method type in your form?

Code:
<form action="checkDemo.php" method="get">
 
Only two things caught my attention (I'm not all that with PHP so bare with me):

Firstly, you didn't close this tag:

Code:
<input type="submit"

Secondly, I think you're supposed to specify between post and get data using the method attribute, which you missed here:

Code:
    <form action = "checkDemo.php">

which should specify between "post" or "get"

Sorry if I've completely missed the point or something though. :p

Edit: Curses, beaten.
 
I've fixed those two problems, cheers guys.
Still having the problem with the $total variable though. It should be getting the associated values from the variables and adding them to the $total variable and outputting it at the end. Everything else is working bar this...
 
Problem....
Code:
$total = $total + chkFries;
Should be:

Code:
[FONT=monospace] $total = $total + [B]$[/B]chkFries;[/FONT]

Repeat for all and it works.

 
Last edited:
Good luck with PHP matey, its a great language. Its missing a few features I would like, but otherwise very pleased with it :D

Makes what I use at work (J2EE) look like a dog...
 
Back
Top Bottom