input type=checkbox name needs to access javascript function AND upload for php form processing

57 Views Asked by At

The problem is capturing the “name“ variable for the PHP processing, as well as sending it to the JavaScript function function checkTotal(). “choice” is used as the variable to pass to the JavaScript function. "choice" has to be used as the form-name variable to get the correct subtotal which does work. "choice" must be unique however. when uploading to php form processing.php. The

I have used the input - hidden to pass the names of the items up OK. Whatever "choice" (cost) was last equal to is what gets sent up. Choice needs to be a unique variable to correctly process but this will NOT subtotal correctly in the JavaScript function.

Here is the code (main.php) with the JavaScript subtotal working but NOT the PHP form processor:

<?php
session_start();  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="Description" content="Your description here...">
<meta name="Keywords" content="Your keywords here...">
<script>
function checkTotal() {                                     //   
document.listForm.total.value = '';
var sum = 0;     
var o = 0;
for (i=0;i<document.listForm.choice.length;i++) {
   if (document.listForm.choice[i].checked) {
      sum = $priceaa  = sum + parseFloat(document.listForm.choice[i].value);
         if(document.getElementById("Checkbox22").checked==true)
            o++;
         }
    }
if (o>1) {sum=sum-0;}
   document.listForm.total.value = sum;           
}
</script>
</head>

<body><input type="hidden" name="formid" value="listForm"> <form name="listForm" method="post"         action="processing.php">
<?php
//Simple Form that needs to processed w/ php:
$_SESSION['choice'] = 0;
$pricea = 7.00;    ///  small
$_SESSION['pricea'] = $pricea;     //      set  to check in processing.php against choice
$pricec = 10.00;  ///  large
$_SESSION['pricec'] = $pricec;
$price1101 = 5.00;        // shrimp price                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
$_SESSION['price1101'] = $price1101;
$price1201 = 3.00;       //bacon price
$_SESSION['price1201'] = $price1201 ;  
$ingred101 = "SHRIMP";    //  uploaded by hidden input
$ingred201 = "BACON";     //  uploaded by hidden input
$top = 30;
$left = 30;
/////////////   RADIO  BUTTONS   //////////////////////////   Small  size
echo "<div id=\"wb_Text6\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:70px;height:42px;z-index:13;\">     <span style=\"color:#000000;font-family:Arial;font-size:19px;\">Small<br>$".$pricea."</span>   </div>\n";
$left =   $left + 60;
echo "<div id=\"wb_RadioButton1\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:54px;height:39px;z-index:50;\">  <input type=\"radio\" name=\"choice\" value=".$pricea." onChange=\"checkTotal()\"/>  <label for=\"RadioButton1\"></label></div>\n";
$left =   $left + 60;
/////////////////////////////////////////////////////////////// Large size
echo "<div id=\"wb_Text9\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:70px;height:42px;z-index:7;\"><span style=\"color:#000000;font-family:Arial;font-size:19px;\">Large<br>$".$pricec." </span></div>\n";
$left =   $left + 60;
echo "<div id=\"wb_RadioButton1\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:54px;height:39px;z-index:50;\"><input type=\"radio\" name=\"choice\" value=".$pricec."  onChange=\"checkTotal()\"/><label for=\"RadioButton1\"></label></div>\n";
/////////////////////////////////////////////////////////////$price1101  being sent to   processing.php  using form name "choice"
$top = $top + 90;
$left = $left - 170;
////////////////////            CHECK BOXES   ///////////////////
/////////////////////////////////////////////////////////////////  SHRIMP   
echo "<div id=\"wb_Text2\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:192px;height:253px;z-index:13;\"><span style=\"color:#000000;font-family:Arial;font-size:19px;\">SHRIMP($5)<br></span></div>\n"; 
$left =   $left + 110;
echo "<div id=\"wb_Checkbox2\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:39px;height:40px;z-index:20;\">  <input type=\"checkbox\" id=\"Checkbox22\" name=\"choice\" value=".$price1101."  onChange=\"checkTotal()\"/><label for=\"Checkbox3\"></div>\n";
$left =   $left + 90;
///////////////////////////////////////////////////////////// //  the hidden item name sent  up to processing.php
echo "<div id=\"wb_Checkbox22\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:39px;height:40px;z-index:15;\"><input type=\"hidden\" id=\"Checkbox22\" name=\"add_extra101\" value=\"".$ingred101."\" ><label for=\"Checkbox22\"></label></div>\n";         //onChange=\"checkTotal()\"/
///////////////////////////////////////////////////////////$price1201  being sent to processing.php  using form name "choice"
$left = $left + 70;   
////////////////////////////////////////////////////////////////////  BACON
echo "<div id=\"wb_Text2\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:192px;height:253px;z-index:13;\"><span style=\"color:#000000;font-family:Arial;font-size:19px;\"> BACON($3)<br></span></div>\n"; 
$left = $left + 105;   
echo "<div id=\"wb_Checkbox2\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:39px;height:40px;z-index:20;\"> <input type=\"checkbox\" id=\"Checkbox22\" name=\"choice\" value=".$price1201." onChange=\"checkTotal()\"/><label for=\"Checkbox3\"></div>\n"; 
////////////////////////////////// //  the hidden item name sent  up to processing.php
echo "<div id=\"wb_Checkbox22\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:39px;height:40px;z-index:15;\"><input type=\"hidden\" id=\"Checkbox22\" name=\"add_extra201\" value=\"".$ingred201."\" ><label for=\"Checkbox22\"></label></div>\n";         //don't needonChange=\"checkTotal()\"/
//////////end php
$left = $left - 375;   
$top = $top + 50;
//////////////////////////////////////////////////////////////////////  Subtotal       
echo "<div id=\"wb_Text2\" style=\"position:absolute;left:".$left."px;top:".$top."px;width:192px;height:253px;z-index:13;\"><span style=\"color:#000000;font-family:Arial;font-size:19px;\">SubTotal: </span>
<span style=\"color:#A52A2A;font-family:Arial;font-size:24px;\"><input type=\"text\" size=\"18\" name=\"total\" value=\"".$_SESSION['choice']."\"/> </span></div>\n";
?>    
<!-- ///////////////////////////////////////////////////////////////  submit Button -->
<div id="Layer1" style="position:absolute;text-align:right;left:30px;top:251px;width:135px;height:6.147%;z-index:69;">
<input type="submit" id="Button1" name="" value="ADD to CART" style="display:inline-block;width:145px;height:55px;z-index:0;"></div>
</form>
</body>
</html>

and the processing.php:


<?php
session_start();  
if ($_SERVER['REQUEST_METHOD'] == 'POST'){    
  $choice = $_REQUEST['choice'];                  
  echo "choiceeeeee".$choice;  //  $choice is always the last selection made  needs to be unique  is  used
  echo "<br>";
  if($_SESSION['pricea'] ==  $_REQUEST['choice']){            //choice  needs to be unique --  now always the last "choice"  selected, will fail if 2 prices are the same
     echo "Small Size-$". $_SESSION['pricea'];
     $price_large = $_SESSION['pricea'];
     $price_small = 0.00;
     echo "<br>";
  } else {                              
     $price_large =0.00;                    //  forced to zero,  otherwise undefined
     $price_small = 0.00;
  }
  if($_SESSION['pricec'] ==  $_REQUEST['choice']){         //choice  needs to be unique --  now always the last "choice"  selected is  used. will fail if 2 prices are the same
     echo "Large_Size-$". $_SESSION['pricec'];
     $price_small = $_SESSION['pricec'];
     $price_large = 0.00;
     echo "<br>";
  } else {                              
     $price_large = 0.00;              //  forced to zero,  otherwise undefined
     $price_small = 0.00;      
  }
  if($_SESSION['price1101'] == $_REQUEST['choice']){     //choice  needs to be unique --  now always the last "choice"  selected  is  used  will fail if 2 prices are the same
     $price_shrimp = $_SESSION['price1101'];
  } else {      
     $price_shrimp = 0.00;
  }  
  if($_SESSION['price1201'] == $_REQUEST['choice']){         //choice  needs to be unique --  now always the last "choice"  selected  is  used, will fail if 2 prices are the same
     $price_bacon = $_SESSION['price1201'];      
  } else {      
     $price_bacon = 0.00;
  }         
  echo " price_shrimp". $price_shrimp;
  echo "<br>";
  echo " price_bacon". $price_bacon;
  $subtot = $price_shrimp +  $price_bacon + $price_large + $price_small;  
  echo "<br>";
  echo "subtot".$subtot;
  echo "<a href=\"https://foonow.com/stellas/main.php>REFRESH</a>";   
}      
?>

Unfortunately, I know a little bit about PHP & mySQL, but not much about JavaScript, so I greatly appreciate anyone that could help.

1

There are 1 best solutions below

1
Феоктист Пупырышкин On

maybe this will help you...

try use brackets inside name of elements

...
<input type=\"radio\" name=\"choice[]\" value=".$pricea." onChange=\"checkTotal()\"/>
<input type=\"radio\" name=\"choice[]\" value=".$pricec."  onChange=\"checkTotal()\"/>
<input type=\"checkbox\" id=\"Checkbox22\" name=\"choice[]\" value=".$price1101."  onChange=\"checkTotal()\"/>

fix it a little javascript

function checkTotal() {                                     //...
document.listForm.total.value = '';
var sum = 0;.....
var o = 0;
var choices = document.getElementsByName('choice[]');
for (i=0;i<choices.length;i++) {
   if (choices[i].checked) {
      sum = $priceaa  = sum + parseFloat(choices[i].value);
         if(document.getElementById("Checkbox22").checked==true)
            o++;
         }
    }
if (o>1) {sum=sum-0;}
   document.listForm.total.value = sum;...........
}

and display the result after submitting the form

...
session_start();..
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
var_dump( $_POST['choice'] );
}
...