•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,611 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 55023 | Replies: 6
![]() |
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Rep Power: 4
Solved Threads: 0
I have a problem with php and checkbox. Can someone
help me with it?
I want to be able to compute values of a checkbox that is when the
checkboxes are checked using php.
example
<input type="checkbox" name="course" value="¢30 /> JAVA
<input type="checkbox" name="course" value="¢100 /> C
<input type="checkbox" name="course" value="¢10 /> PERL
<input type="checkbox" name="course" value="¢200 /> C++
<input type="checkbox" name="course" value="¢70 /> PHP
<input type="checkbox" name="course" value="¢90 /> C#
so in that case when i check C, JAVA and PHP,
the php code should be able to compute the values in cedis for me
as a user and given a summary of the course chosen and also
the total amount involved with each course chosen.
so that i have say
This is a summary of the courses registered for
JAVA 30
C 100
PHP 70
and the total amount to be paid is 200.
So what i want is the php code to help me do that.
help me with it?
I want to be able to compute values of a checkbox that is when the
checkboxes are checked using php.
example
<input type="checkbox" name="course" value="¢30 /> JAVA
<input type="checkbox" name="course" value="¢100 /> C
<input type="checkbox" name="course" value="¢10 /> PERL
<input type="checkbox" name="course" value="¢200 /> C++
<input type="checkbox" name="course" value="¢70 /> PHP
<input type="checkbox" name="course" value="¢90 /> C#
so in that case when i check C, JAVA and PHP,
the php code should be able to compute the values in cedis for me
as a user and given a summary of the course chosen and also
the total amount involved with each course chosen.
so that i have say
This is a summary of the courses registered for
JAVA 30
C 100
PHP 70
and the total amount to be paid is 200.
So what i want is the php code to help me do that.
•
•
Join Date: Jan 2005
Location: Sheffield, UK
Posts: 294
Reputation:
Rep Power: 4
Solved Threads: 6
You should change the checkbox names, they are all the same now (each checkbox name has to be unique). see my other post of identical question at http://www.daniweb.com/techtalkforum...340#post156340
Once your script know which course the user chose, you can do some math to sum up the fee based on post data.
Once your script know which course the user chose, you can do some math to sum up the fee based on post data.
Ecommerce-Web-Store.com Building Your e-Business.
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Rep Power: 4
Solved Threads: 0
Thank you all for ur help. But i did this and is still not working.
HTML CODE
.........
<form action="testcheckbox.php" method="POST" name="form1">
<p>Name :
<input type="text" name="textfield">
</p>
<p>Course:
<input type="text" name="textfield">
</p>
<p>Tel No:
<input type="text" name="textfield">
</p>
<p>
<input type="checkbox" name="cd[]" value="¢20">
JAVA<br>
<input type="checkbox" name="cd[]" value="¢30">
PERL<br>
<input type="checkbox" name="cd[]" value="¢10">
PYTHON<br>
<input type="checkbox" name="cd[]" value="¢30">
C#<br>
<input type="checkbox" name="cd[]" value="¢90">
JYTHON<br>
<input type="checkbox" name="cd[]" value="¢100">
C++<br>
<input type="checkbox" name="cd[]" value="¢120">
PHP</p>
<p align="center">
<input type="submit" name="Submit" value="Join Now">
</p>
<p align="left"> <br>
</p>
</form>
PHP CODE FOR ACTION
..........................
<?php
$totalprice = 0;
foreach($cd as $cd)
$totalprice += $cd;
echo "$totalprice";
/*if(isset($_POST['$cd']))
echo 'checked';*/
?>
Because i get the results to be 0 always.
But i would want it to compute all the courses that would checked
and the total for their price listed.
HTML CODE
.........
<form action="testcheckbox.php" method="POST" name="form1">
<p>Name :
<input type="text" name="textfield">
</p>
<p>Course:
<input type="text" name="textfield">
</p>
<p>Tel No:
<input type="text" name="textfield">
</p>
<p>
<input type="checkbox" name="cd[]" value="¢20">
JAVA<br>
<input type="checkbox" name="cd[]" value="¢30">
PERL<br>
<input type="checkbox" name="cd[]" value="¢10">
PYTHON<br>
<input type="checkbox" name="cd[]" value="¢30">
C#<br>
<input type="checkbox" name="cd[]" value="¢90">
JYTHON<br>
<input type="checkbox" name="cd[]" value="¢100">
C++<br>
<input type="checkbox" name="cd[]" value="¢120">
PHP</p>
<p align="center">
<input type="submit" name="Submit" value="Join Now">
</p>
<p align="left"> <br>
</p>
</form>
PHP CODE FOR ACTION
..........................
<?php
$totalprice = 0;
foreach($cd as $cd)
$totalprice += $cd;
echo "$totalprice";
/*if(isset($_POST['$cd']))
echo 'checked';*/
?>
Because i get the results to be 0 always.
But i would want it to compute all the courses that would checked
and the total for their price listed.
•
•
Join Date: Jan 2005
Location: Sheffield, UK
Posts: 294
Reputation:
Rep Power: 4
Solved Threads: 6
Your checkbox should look like this:
<input type="checkbox" name="cd0" value="20">x
<input type="checkbox" name="cd1" value="30">y
<input type="checkbox" name="cd2" value="100">z
php part, let say you have three checkboxes:
[php]$total = ;
for($i=0;$i<3;$i++){
$amount = $_POST['cd'.$i];
$total += $amount;
}
echo $total;[/php]
<input type="checkbox" name="cd0" value="20">x
<input type="checkbox" name="cd1" value="30">y
<input type="checkbox" name="cd2" value="100">z
php part, let say you have three checkboxes:
[php]$total = ;
for($i=0;$i<3;$i++){
$amount = $_POST['cd'.$i];
$total += $amount;
}
echo $total;[/php]
Ecommerce-Web-Store.com Building Your e-Business.
•
•
Join Date: Jan 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
You should change the checkbox names, they are all the same now (each checkbox name has to be unique). see my other post of identical question at http://www.daniweb.com/techtalkforum...tml#post156340
Once your script know which course the user chose, you can do some math to sum up the fee based on post data.
Actually, each checkbox name does not have to be unique. In fact you can have something such as:
[HTML]
<input type='checkbox' name='myCheckbox' value='1'>
<input type='checkbox' name='myCheckbox' value='2'>
<input type='checkbox' name='myCheckbox' value='3'>
<input type='checkbox' name='myCheckbox' value='4'>
[END HTML]
[PHP]
/***********
$_REQUEST['myCheckbox'] will equal an array of selected values.
***********/
foreach($_REQUEST['myCheckbox'] as $singleVar)
{
//$singleVar will equal the value of a selected checkbox,
}
[END PHP]
Maybe I didn't read the thread thoroughly enough... I apologize if I missed something.
-Adam
•
•
Join Date: Aug 2006
Posts: 138
Reputation:
Rep Power: 3
Solved Threads: 2
•
•
•
•
Thank you all for ur help. But i did this and is still not working.
HTML CODE
.........
<form action="testcheckbox.php" method="POST" name="form1">
<p>Name :
<input type="text" name="textfield">
</p>
<p>Course:
<input type="text" name="textfield">
</p>
<p>Tel No:
<input type="text" name="textfield">
</p>
<p>
<input type="checkbox" name="cd[]" value="¢20">
JAVA<br>
<input type="checkbox" name="cd[]" value="¢30">
PERL<br>
<input type="checkbox" name="cd[]" value="¢10">
PYTHON<br>
<input type="checkbox" name="cd[]" value="¢30">
C#<br>
<input type="checkbox" name="cd[]" value="¢90">
JYTHON<br>
<input type="checkbox" name="cd[]" value="¢100">
C++<br>
<input type="checkbox" name="cd[]" value="¢120">
PHP</p>
<p align="center">
<input type="submit" name="Submit" value="Join Now">
</p>
<p align="left"> <br>
</p>
</form>
PHP CODE FOR ACTION
..........................
<?php
$totalprice = 0;
foreach($cd as $cd)
$totalprice += $cd;
echo "$totalprice";
/*if(isset($_POST['$cd']))
echo 'checked';*/
?>
Because i get the results to be 0 always.
But i would want it to compute all the courses that would checked
and the total for their price listed.
<form action="testcheckbox.php" method="POST" name="form1"> <p>Name : <input type="text" name="textfield"> </p> <p>Course: <input type="text" name="textfield"> </p> <p>Tel No: <input type="text" name="textfield"> </p> <p> <input type="checkbox" name="cd[]" value="20"> JAVA<br> <input type="checkbox" name="cd[]" value="30"> PERL<br> <input type="checkbox" name="cd[]" value="10"> PYTHON<br> <input type="checkbox" name="cd[]" value="30"> C#<br> <input type="checkbox" name="cd[]" value="90"> JYTHON<br> <input type="checkbox" name="cd[]" value="100"> C++<br> <input type="checkbox" name="cd[]" value="120"> PHP</p> <p align="center"> <input type="submit" name="Submit" value="Join Now"> </p> <p align="left"> <br> </p> </form>
php Syntax (Toggle Plain Text)
<?php $totalprice = 0; foreach($_POST['cd'] as $cd) $totalprice += $cd; echo "$totalprice"; ?>
Last edited by php_daemon : Jan 15th, 2007 at 12:44 pm.
•
•
Join Date: Jan 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
<form action="testcheckbox.php" method="POST" name="form1"> <p>Name : <input type="text" name="textfield"> </p> <p>Course: <input type="text" name="textfield"> </p> <p>Tel No: <input type="text" name="textfield"> </p> <p> <input type="checkbox" name="cd[]" value="20"> JAVA<br> <input type="checkbox" name="cd[]" value="30"> PERL<br> <input type="checkbox" name="cd[]" value="10"> PYTHON<br> <input type="checkbox" name="cd[]" value="30"> C#<br> <input type="checkbox" name="cd[]" value="90"> JYTHON<br> <input type="checkbox" name="cd[]" value="100"> C++<br> <input type="checkbox" name="cd[]" value="120"> PHP</p> <p align="center"> <input type="submit" name="Submit" value="Join Now"> </p> <p align="left"> <br> </p> </form>php Syntax (Toggle Plain Text)
<?php $totalprice = 0; foreach($_POST['cd'] as $cd) $totalprice += $cd; echo "$totalprice"; ?>
Depending on your PHP configuration $_POST['cd'] and $cd could potentially reference the same variables. Try changing your foreach statement to :
foreach($_POST['cd'] as $singleCD)
Also, one thing that I have found useful while doing server side processing with PHP is to actually view the array keys of the POST variable.
You can do so with the array_keys() function.
foreach(array_keys($_POST) as $myKey)
echo $myKey."<br />";
I have seen situations before where the browser can prematurely close a form (prior to the closing form tag) and elements such as checkboxes and text fields can be completely ignored. If changing the variable names within the foreach does not work, verify that you're actually receiving the variables as part of the POST array. Does this make sense?
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
Similar Threads
- How to use HTML tags in PHP (PHP)
- checkbox values calculation with PHP (PHP)
- Php newsletter error (PHP)
Other Threads in the PHP Forum
- Previous Thread: Display Auto_increment Value
- Next Thread: help with recursion, for experts


Linear Mode