ctmarco3 Posted January 20, 2012 Share Posted January 20, 2012 Hello all I am working on creating a basic ordering form that allow the user to use check-boxes to select certain products and than return a total back to them. I am having a hard time figuring out the php code to gather and calculate things. What I dont quite understand is how to pass the data properly so my $sum calculation actually knows what the values are... In my html doc I have my checkboxes as follows: //formatted in tables <input type = "checkbox" name = "eight100wattreg" size ="40" /> <input type = "checkbox" name = "four100wattreg" size ="40" /> //etc.... and in my php doc: <?php> //get form values $four100wattreg = $_POST["four100wattreg"]; $eight100wattreg = $_POST["eight100wattreg"]; $four100wattlong = $_POST["four100wattlong"]; $eight100wattlong = $_POST["eigth100wattlong"]; $payment = $_POST["payment"]; //set lightbulb values $four100wattreg=2.39; $eight100wattreg=2.39; $four100wattlong=2.39; $eight100wattlong=2.39; //calculate costs if (isset($_POST['four100wattreg']) { //checkbox is checked $four100wattreg = $_POST['four100wattreg']; } else if (!isset($_POST['checkbox']) { //checkbox not checked $four100wattreg = 0; } $sum = $four100wattreg + $eight100wattreg + $four100wattlong + $eigth100wattlong; ?> <?php> echo "Your total is: '$sum' <br />"; ?> I also attached the files if someone wants to look at them in they're entirety. Any advice would be greatly appreciated, but please keep in mind I am a php beginner and am still learning. Thanks, Charlie 17386_.txt 17387_.php Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/ Share on other sites More sharing options...
thomasw_lrd Posted January 20, 2012 Share Posted January 20, 2012 $four100wattreg=2.39; You're using the same variables for the cost as the checkbox. You should rewrite them as $four100wattregcost. Also, this line echo "Your total is: '$sum' <br />" Should be echo "Your total is: ".$sum." <br />" Adding the dots is like concatenating it all into one big string. Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309640 Share on other sites More sharing options...
AyKay47 Posted January 20, 2012 Share Posted January 20, 2012 $four100wattreg=2.39; You're using the same variables for the cost as the checkbox. You should rewrite them as $four100wattregcost. to explain this post further so you understand why it is not correct, when you set the variable $four100wattreg = $_POST["four100wattreg"]; and then again $four100wattreg=2.39; you are overwriting the first value that you set $four100wattreg to with the new value (2.39). You should be using unique variable names for each unique value that you want PHP to remember. if (!isset($_POST['checkbox']) { //checkbox not checked $four100wattreg = 0; } where is $_POST['checkbox'] being passed to the server? Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309642 Share on other sites More sharing options...
Psycho Posted January 20, 2012 Share Posted January 20, 2012 Also, this line echo "Your total is: '$sum' <br />" Should be echo "Your total is: ".$sum." <br />" Adding the dots is like concatenating it all into one big string. There is nothing wrong with what he has. The variable will be parsed inside the double quoted string. Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309648 Share on other sites More sharing options...
ctmarco3 Posted January 20, 2012 Author Share Posted January 20, 2012 OK that makes sense, thank you everyone for the replies. What I don't get than is how to get the value for my variable pushed to my $sum command. Is there a way to say do something like this perhaps?: If the box is checked than my $four100wattreg = 2.39, if the box is unchecked than $four100wattreg = 0 What would be the best way to handle that? $four100wattreg=2.39; You're using the same variables for the cost as the checkbox. You should rewrite them as $four100wattregcost. to explain this post further so you understand why it is not correct, when you set the variable $four100wattreg = $_POST["four100wattreg"]; and then again $four100wattreg=2.39; you are overwriting the first value that you set $four100wattreg to with the new value (2.39). You should be using unique variable names for each unique value that you want PHP to remember. if (!isset($_POST['checkbox']) { //checkbox not checked $four100wattreg = 0; } where is $_POST['checkbox'] being passed to the server? Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309658 Share on other sites More sharing options...
thomasw_lrd Posted January 20, 2012 Share Posted January 20, 2012 Yes and no. Inside you're if statement you are posting the checkbox value. What you would want to do is something like this. //calculate costs if (isset($_POST['four100wattreg']) { //checkbox is checked $four100wattreg = 2.39; } else if (!isset($_POST['four100wattreg']) { //checkbox not checked $four100wattreg = 0; } I believe that is correct, but I could be wrong. I didn't test it. I'm sure some of the better programmers on here may have a better solution. Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309660 Share on other sites More sharing options...
Psycho Posted January 20, 2012 Share Posted January 20, 2012 I'm not going to go through and rewrite all your code. But, you should set up your data into an array or database. Then use that for all your processing: A very simple format $products = array( 'Four 100 Watt Reg' => 2.39, 'Eight 100 Watt Reg' => 2.39, 'Four 100 Watt Long' => 2.39, 'Four 100 Watt Long' => 2.39, ) Then you can use that data to: create the input fields and process the data. I would create the input fields something like this so they are an array (much easier to process) foreach($products as $prodName => $price) { echo "<input type='checkbox' name='products[]' value='$prodName'> $prodName<br>\n"; } Then to process the post data into a total $total = 0; foreach($_POST['products'] as $prodName) { $total += $products[$prodName]; } Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309662 Share on other sites More sharing options...
vivekanand25 Posted January 20, 2012 Share Posted January 20, 2012 use this pages to see the display in ur page 17388_.php 17389_.php Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309667 Share on other sites More sharing options...
ctmarco3 Posted January 20, 2012 Author Share Posted January 20, 2012 I'm not going to go through and rewrite all your code. But, you should set up your data into an array or database. Then use that for all your processing: A very simple format $products = array( 'Four 100 Watt Reg' => 2.39, 'Eight 100 Watt Reg' => 2.39, 'Four 100 Watt Long' => 2.39, 'Four 100 Watt Long' => 2.39, ) Then you can use that data to: create the input fields and process the data. I would create the input fields something like this so they are an array (much easier to process) foreach($products as $prodName => $price) { echo "<input type='checkbox' name='products[]' value='$prodName'> $prodName<br>\n"; } Then to process the post data into a total $total = 0; foreach($_POST['products'] as $prodName) { $total += $products[$prodName]; } I wondered about using array from the research I had done but wasn't quite sure how to implement it, let me do some additional research and see if I can take your advice and actually integrate it into my form. Thanks so much! I may be back, but hopefully not. Quote Link to comment https://forums.phpfreaks.com/topic/255440-how-to-gather-data-from-a-checkbox-form/#findComment-1309687 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.