954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Checkbox

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

egoleo
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
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/techtalkforums/showthread.php?p=156340#post156340

Once your script know which course the user chose, you can do some math to sum up the fee based on post data.

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

Thank you all for ur help. But i did this and is still not working.

HTML CODE
.........

Name :

Course:

Tel No:



JAVA

PERL

PYTHON

C#

JYTHON

C++

PHP




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.

egoleo
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Your checkbox should look like this:
x
y
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]

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

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/techtalkforums/post156340.html#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]

[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

amschroeder
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Thank you all for ur help. But i did this and is still not working.

HTML CODE .........

Name :

Course:

Tel No:

JAVA
PERL
PYTHON
C#
JYTHON
C++
PHP


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
<input type="checkbox" name="cd[]" value="30">
PERL
<input type="checkbox" name="cd[]" value="10">
PYTHON
<input type="checkbox" name="cd[]" value="30">
C#
<input type="checkbox" name="cd[]" value="90">
JYTHON
<input type="checkbox" name="cd[]" value="100">
C++
<input type="checkbox" name="cd[]" value="120">
PHP</p>
<p align="center">
<input type="submit" name="Submit" value="Join Now">
</p>
<p align="left"> 
</p>
</form>
<?php
        $totalprice = 0;
        foreach($_POST['cd'] as $cd) $totalprice += $cd;
        echo "$totalprice";
?>
php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 
<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
<input type="checkbox" name="cd[]" value="30">
PERL
<input type="checkbox" name="cd[]" value="10">
PYTHON
<input type="checkbox" name="cd[]" value="30">
C#
<input type="checkbox" name="cd[]" value="90">
JYTHON
<input type="checkbox" name="cd[]" value="100">
C++
<input type="checkbox" name="cd[]" value="120">
PHP</p>
<p align="center">
<input type="submit" name="Submit" value="Join Now">
</p>
<p align="left"> 
</p>
</form>
<?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."
";


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?

amschroeder
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You