返回

如何在 PHP 和 JavaScript 函数中同时处理输入?

javascript

如何在 PHP 和 JavaScript 函数中同时处理输入

问题

我正在同时处理 PHP 和 JavaScript 函数中的用户输入,但我需要将项目名称发送给 JavaScript 函数 checkTotal()

解决方法

1. 使用隐藏输入

隐藏输入字段可以传递项目名称,而无需更改名称变量。在 main.php 中添加以下代码:

<input type="hidden" name="add_extra101" value="<?php echo $ingred101; ?>">

2. 使用唯一变量

为每个选项分配一个唯一的变量作为名称。在 main.php 中,将选项值设置为相应项目的价格:

<input type="radio" name="choice-small" value="<?php echo $pricea; ?>" onChange="checkTotal()">

修改后的代码

main.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 = 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>
<?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 = 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-small\" 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-large\" 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-shrimp\" 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-bacon\" 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>
quot;.$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-small\" 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>
<?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 = 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-small\" 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-large\" 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-shrimp\" 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-bacon\" 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>
quot;.$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-large\" 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-shrimp\" 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-bacon\" 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>

结论

通过使用隐藏输入或唯一变量,您可以同时为 PHP 处理和 JavaScript 函数 capture 项目