PHP Form with Checkboxes and Addition

  • JohnH
  • Born
  • Born
  • JohnH
  • Posts: 2
  • Loc: Minnesota

Post 3+ Months Ago

Hello. I am trying create a form with checkboxes that contain different values. Each checkbox is supposed to be worth a different number and once the user submits the form the php code is supposed to add each number that was checked and give a total, then multiply it by 55. However, I am stuck. What I have gotten to so far is a basic html form and what I believe is the start to the php code. Here is the html form:
HTML Code: [ Select ]
<html>
<head>
<title>First Page
</title>
</head>
<body> Here is some text.
<form action="calc.php" method="post">
This value is 3 <input type="checkbox" name="general1">
This value is 4 <input type="checkbox" name="general2">
This value is 5 <input type="checkbox" name="general3">
This value is 6 <input type="checkbox" name="general4">
This value is 7 <input type="checkbox" name="general5">
<input type="submit">
 </form>
 </body>
 </html>
  1. <html>
  2. <head>
  3. <title>First Page
  4. </title>
  5. </head>
  6. <body> Here is some text.
  7. <form action="calc.php" method="post">
  8. This value is 3 <input type="checkbox" name="general1">
  9. This value is 4 <input type="checkbox" name="general2">
  10. This value is 5 <input type="checkbox" name="general3">
  11. This value is 6 <input type="checkbox" name="general4">
  12. This value is 7 <input type="checkbox" name="general5">
  13. <input type="submit">
  14.  </form>
  15.  </body>
  16.  </html>


And here is my attempt at creating the php code:
PHP Code: [ Select ]
<html>
<head>
<title>Second Page</title>
</head>
<body>
<?php
if (isset($HTTP_POST_VARS)) {
$general1 = $HTTP_POST_VARS["general1"];
$general2 = $HTTP_POST_VARS["general2"];
$general3 = $HTTP_POST_VARS["general3"];
$general4 = $HTTP_POST_VARS["general4"];
$general5 = $HTTP_POST_VARS["general5"];
if (count($general1)>0)
{
$general1 = 3;
}
if (count($general2)>0)
{
$general2 = 4;
}
if (count($general3)>0)
{
$general3 = 5;
}
if (count($general4)>0)
{
$general4 = 6;
}
if (count($general5)>0)
{
$general5 = 7;
}
function add($general1,$general2,$general3,$general4,$general5)
{
$total=$general1+$general2+$general3+$general4+$general5;
return $total;
}
echo "Your total is $total";
}
?>
</body>
</html>
 
  1. <html>
  2. <head>
  3. <title>Second Page</title>
  4. </head>
  5. <body>
  6. <?php
  7. if (isset($HTTP_POST_VARS)) {
  8. $general1 = $HTTP_POST_VARS["general1"];
  9. $general2 = $HTTP_POST_VARS["general2"];
  10. $general3 = $HTTP_POST_VARS["general3"];
  11. $general4 = $HTTP_POST_VARS["general4"];
  12. $general5 = $HTTP_POST_VARS["general5"];
  13. if (count($general1)>0)
  14. {
  15. $general1 = 3;
  16. }
  17. if (count($general2)>0)
  18. {
  19. $general2 = 4;
  20. }
  21. if (count($general3)>0)
  22. {
  23. $general3 = 5;
  24. }
  25. if (count($general4)>0)
  26. {
  27. $general4 = 6;
  28. }
  29. if (count($general5)>0)
  30. {
  31. $general5 = 7;
  32. }
  33. function add($general1,$general2,$general3,$general4,$general5)
  34. {
  35. $total=$general1+$general2+$general3+$general4+$general5;
  36. return $total;
  37. }
  38. echo "Your total is $total";
  39. }
  40. ?>
  41. </body>
  42. </html>
  43.  


I just can't seem to get it to display a result though.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

$HTTP_POST_VARS is a global variable and by default most servers turn them off. You can get the same data by using the $_POST variable.

PHP Code: [ Select ]
if (isset($_POST)) {
$general1 = $_POST["general1"];
 
  1. if (isset($_POST)) {
  2. $general1 = $_POST["general1"];
  3.  

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 107 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.