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