boom_roasted Posted January 27, 2010 Share Posted January 27, 2010 A user is supposed to check the boxes on what they want in their order(checkdemo.html). Then when they submit it, the php file should tell them what they ordered and also it is supposed to total up the cost of all the selected items(checkdemo.php). Screenshot of checkdemo.html: THIS IS CHECKDEMO.HTML <html> <head> <title>Checkbox Demo</title> </head> <body> <h1>Checkbox</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> THIS IS CHECKDEMO.PHP <html> <head> <title>Checkbox Demo</title> </head> <body> <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> Here is what comes up when I submit the html file (having all checkboxes checked: Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/ Share on other sites More sharing options...
RussellReal Posted January 27, 2010 Share Posted January 27, 2010 first you need to get the values.. try $chkFries = $_REQUEST['chkFries']; and all the others for all the others. Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002198 Share on other sites More sharing options...
teamatomic Posted January 27, 2010 Share Posted January 27, 2010 1. In the checkdemo file <? $chkFries = $_POST['chkFries']; /// do this for every checkbox or any other form widgets you choose to make. print <<<HERE 2. to your form tag add: method="POST" 3. examine some form tutorials. You need to understand the logic and various methods used to process forms. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002201 Share on other sites More sharing options...
jmr3460 Posted January 27, 2010 Share Posted January 27, 2010 I believe your form checkboxes should have the same name like name="order" and your value should define the value of the checkbox, value="fries". <form method="post" action="do_form.php> <input type="checkbox" name="order[]" value="fries"> Fries <input type="checkbox" name="order[]" value="hamburger"> Hamburger <input type="checkbox" name="order[]" value="coke"> Coke <input type="submit" name="submit" value="SUBMIT ORDER"> This form is very simple html. Then you should create a php script that would process the order searching a database table for the field of fries, then selecting a field in that row which would have the price of that product. See if this gets you started in the right direction. Good Luck. Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002204 Share on other sites More sharing options...
boom_roasted Posted January 27, 2010 Author Share Posted January 27, 2010 1. In the checkdemo file <? $chkFries = $_POST['chkFries']; /// do this for every checkbox or any other form widgets you choose to make. print <<<HERE 2. to your form tag add: method="POST" 3. examine some form tutorials. You need to understand the logic and various methods used to process forms. HTH Teamatomic When I do all that and rerun it, I get an error: Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in E:\indigoampp\apache-2.2.11\htdocs\ph03\checkDemo.php on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002209 Share on other sites More sharing options...
boom_roasted Posted January 27, 2010 Author Share Posted January 27, 2010 Line 12 happens to be: $chkFries = $_POST['chkFries']; Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002211 Share on other sites More sharing options...
boom_roasted Posted January 27, 2010 Author Share Posted January 27, 2010 OK OK OK. TIME OUT! RESTART! I took out each of the lines like this: $total = $total + $chkFries; And it works (aside from telling the total. Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002213 Share on other sites More sharing options...
boom_roasted Posted January 27, 2010 Author Share Posted January 27, 2010 Please help. I'm going crazy!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002216 Share on other sites More sharing options...
boom_roasted Posted January 27, 2010 Author Share Posted January 27, 2010 Giving this one last try before I give up for the night. Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002221 Share on other sites More sharing options...
teamatomic Posted January 27, 2010 Share Posted January 27, 2010 And we will happily watch as you go crazy unless you show us some code and the error you get. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002230 Share on other sites More sharing options...
chayan19 Posted January 27, 2010 Share Posted January 27, 2010 hello...i have created a PHP script where you can sum up accordingly and display the result ....without using a database....it's workin for me juss check am giving you the page created using dreamweaver... ?php $x=$_POST; $t=0; if(isset($_POST[take])){ if($x['dosa']){ $p=25; $t=$t+$p; } if($x['vada']){ $q=30; $t=$t+$q; } if($x['paw']){ $r=10; $t=$t+$r; } echo "your total is".$t ; } ?> <!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=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <div align="center"> <form id="form1" name="form1" method="post" action=""> <p> </p> <p> <label></label> </p> <table width="323" border="0.2"> <tr> <td width="317">What will you like to have </td> </tr> </table> <table width="325" height="94" border="0.2"> <tr><td width="97">Masala dosa </td> <td width="218"><input type="checkbox" name="dosa" value="checkbox" /></td> </tr> <tr> <td>Vada</td> <td><input type="checkbox" name="vada" value="checkbox" /></td> </tr> <tr> <td>Paw Bhaji </td> <td><input type="checkbox" name="paw" value="checkbox" /></td> </tr> </table> <p> <input type="submit" name="take" value="Submit" /> </p> <p> </p> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002275 Share on other sites More sharing options...
chayan19 Posted January 27, 2010 Share Posted January 27, 2010 i don't think is going to work for large number of check boxes because in that case you will have to write multiple if else statements which is very time consuming does anybody have any other way of doing it.....?/??? Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002276 Share on other sites More sharing options...
RussellReal Posted January 27, 2010 Share Posted January 27, 2010 name="options[fries]" value="1.00" name="options[ketchup]" value="0.50" etc then.. foreach ($_POST['options'] as $key => $value) { echo "{$key} -> {$value}"; } Quote Link to comment https://forums.phpfreaks.com/topic/189939-im-a-beginner-who-needs-help-reading-checkboxes-and-totalling-them-up/#findComment-1002552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.