...

View Full Version : Return values for Checkboxes with same name



AKmiecik
08-09-2008, 04:56 PM
I pulled the following code and created a project cost estimating tool for a carpet cleaning business.

Each name="num" is labeled with a room for the house and I created a second piece of code so that name="num1" determines if they want carpet protectant. (site is http://www.pcsnashville.com/Estimator.php)

How can I return which rooms are checked, which rooms want protectant and along with similar info for number of stairs.

(big time neophite here, thanks.)

Original post: glenngv03-02-2004, 01:53 AM

Origianl Reply: Nitro2 wrote on 03-01-2004 11:36 PM:
...



<html>
<head>
<script type="text/javascript">
function calculate(f)
{
var nums = f.num;
var ntext = f.numtext;
var nitem = f.numitem;
var result = 0;
var items = '';
for(var i=0;i<nums.length;i++)
{
if(nums[i].checked)
{
result+=parseFloat(ntext[i].value);
items+=nitem[i].value+'\n';
}
}
f.answer.value=result;

//if you want to fix to 2 decimal places
//f.answer.value=Number(result).toFixed(2);

f.allitems.value=items;
}
</script>
</head>
<body>
<form name="myform">
Which numbers do you want to add?<br>
<input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="1.50" onchange="calculate(this.form)"><input type="text" name="numitem" value="pencil" onchange="calculate(this.form)"><br>
<input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="2.00" onchange="calculate(this.form)"><input type="text" name="numitem" value="pen" onchange="calculate(this.form)"><br>
<input type="checkbox" name="num" onclick="calculate(this.form)"><input type="text" name="numtext" value="3.20" onchange="calculate(this.form)"><input type="text" name="numitem" value="paper" onchange="calculate(this.form)"><br>
Total <input type="text" name="answer" size="5"><textarea name="allitems" rows="5"></textarea>
</form>
</body>
</html>

AKmiecik
08-09-2008, 05:00 PM
Here's the code I am using



// -->

function calculate(f)
{
var nums = f.num;
var ntext = f.numtext;
var res = 0;
var items = '';
for(var i=0;i<nums.length;i++)
{
if(nums[i].checked)
{
res+=parseFloat(ntext[i].value);
}
}
f.answer.value=Number(res).toFixed(2);
}

function calculate1(f1)
{
var nums1 = f1.num1;
var ntext1 = f1.numtext1;
var test = 0;
var items1 = '';
for(var j=0;j<nums1.length;j++)
{
if(nums1[j].checked)
{
test+=parseFloat(ntext1[j].value);
}
}
f1.answer1.value=Number(test).toFixed(2);
}
function area(form) {
a=eval(form.answer.value)
b=eval(form.answer1.value)
s=eval(form.steps.value)
{
if(b==undefined)
{
form.silver.value = a+s*5
form.gold.value = a*(45/35)+s*5
form.platinum.value = a*(55/35)+s*5
}
else
{

form.silver.value = a+b+s*5
form.gold.value = a*(45/35)+b+s*5
form.platinum.value = a*(55/35)+b+s*5

}
}
}
// End -->

ljr747
08-09-2008, 05:54 PM
Easiest way to return what they want is use something like this:


<?php
if(isset($_POST['submit'])) {
if((!$_POST['checkbox'])) {
echo ('you haven't checked any of the boxes!');
}
} else {
$checkbox = addslashes(htmlspecialchars($_POST['checkbox']));
$insert = mysql_query("INSERT INTO `table` (`checkbox`) VALUES('$checkbox')");
} else {
echo ('<form method='post' action='$_SERVER[PHP_SELF]'>
<input type='checkbox' name='checkbox' value='room1'>
<input type='checkbox' name='checkbox' value='room2'>
<input type='submit' name='submit' value='Submit'>
</form>');
}
?>

Then for grabbing the info of what they have submitted:

<?php
$grab = mysql_query("SELECT * FROM `table`");
while ($display = mysql_fetch_array($grab)) {
echo ('Room number: $display');
}
?>

Thats how I'd do it anyway in FULL php, you can change the values :)
What the first bit does is submit the value of the checkbox to the server so you can view it.
The second bit loops all of the inputs to the server and displays them using the while() statement.

oesxyl
08-10-2008, 02:25 AM
a typo:


echo ('you haven't checked any of the boxes!');


must be:


echo ('you haven\'t checked any of the boxes!');
// or:
echo ("you haven't checked any of the boxes!");


you must check results returned by mysql_query.

regards

AKmiecik
08-10-2008, 12:29 PM
Changing the code like that is not a great opition since I have added logic to do calculations (i..e. involve big rewrite) and I am currently using the "value'=" to hold the cost of cleaning that room.


I'll give it a try, maybe I'll learn something.

masterofollies
08-10-2008, 03:24 PM
I think the easiest way is


$room1 = $_POST["num1"];
$room2 = $_POST["num2"]; //etc

//Then if it emails you the results have it say like

echo "Your customer has chosen $room1,$room2";

That way it'd be blank if they didn't select it, and show a room number if they did.

AKmiecik
08-18-2008, 10:41 PM
Thanks for the suggestion masterofollies but it seems like your solution requires each input tag to have a unique name and they do not.

Progress(?):

The problem has evolved to the point where, by using another input tag for each checkbox on the form (as I noticed the original code has), I can populate a Textarea (in the form) with the a unique value for each box checked.

So now I have two Textareas, each with a set of checkboxes that are selected, now I need to send the values in the Textareas via the submit php file.

Any ideas on how to do that?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum