Jump to content

How to gather data from a checkbox form


ctmarco3

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.