输入类型=复选框名称需要访问javascript函数并上传以进行php表单处理

问题描述 投票:0回答:1

问题是捕获 PHP 处理的“name”变量,并将其发送到 JavaScript 函数 checkTotal()。 “choice”用作传递给 JavaScript 函数的变量。必须使用“choice”作为表单名称变量才能获得有效的正确小计。然而,“选择”必须是独特的。当上传到php表单processing.php时。

我已经使用输入 - 隐藏来传递项目名称,OK。无论“选择”(成本)最后等于什么,都会被发送出去。 Choice 需要是一个唯一的变量才能正确处理,但这不会在 JavaScript 函数中正确小计。

这里是 JavaScript 小计工作的代码 (main.php),但 PHP 表单处理器不工作:

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

和处理.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>";   
}      
?>

不幸的是,我对 PHP 和 mySQL 了解一点,但对 JavaScript 不太了解,所以我非常感谢任何可以提供帮助的人。

javascript php html forms upload
1个回答
0
投票

也许这会对你有帮助...

尝试在元素名称内使用括号

...
<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()\"/>

修复一点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;...........
}

提交表单后显示结果

...
session_start();..
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
var_dump( $_POST['choice'] );
}
...
© www.soinside.com 2019 - 2024. All rights reserved.