0

因此,我正在创建一个注册表单,它根据用户的选择使用 javascript 计算 totalPrice,并返回该值并将其存储为一个类(称为 totalPrice)。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
<script type="text/javascript" src="JavaScript.js"/></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet' type='text/css'>
</head>

<body>
   <form id="paymentform" action="Session5.php" method="post">
   <label><b>Camp Sessions: 1-6 (Check off the week(s) the camper will be participating in.)</b> </label><br /><br />
   <ol>
   <li>July 8 - July 12 ($75/child)<input type=checkbox name="campsessions[]" value="July8-July12" onclick="getTotal()" id="includeweek1"></li>
   <li>July 15 - July 19 ($75/child)<input type=checkbox name="campsessions[]" value="July15-July19" onclick="getTotal()" id="includeweek2"></li>
   <li>July 22 - July 26 ($75/child)<input type=checkbox name="campsessions[]" value="July22-July26" onclick="getTotal()" id="includeweek3"></li>
   <li>July 29 - August 2 ($75/child)<input type=checkbox name="campsessions[]" value="July29-August2" onclick="getTotal()" id="includeweek4"></li>
   <li>August 6 - August 9 ($60/child)<input type=checkbox name="campsessions[]" value="August6-August9" onclick="getTotal()" id="includeweek5"></li>
  <li>August 12 - August 16 ($75/child)<input type=checkbox name="campsessions[]" value="August12-August16" onclick="getTotal()" id="includeweek6"></li>
   </ol>

   <label> <b> Include After Camp Care? </b></label> <input type="checkbox" name= "campcare" onclick="getTotal()" id="aftercampcare" /> <br /><br />
   <i> After Camp Care is available from 4pm-6pm for an additional charge of $2/hr.</i><br /><br />

      Total Price:<div class="inline totalPrice"> </div>
      <br/>



<input type="submit" name="formSubmit" value="Submit" class="button greenButton"/>
<input type="button" name="cancel" value="Cancel" class="button redButton" onclick="href='activemindandbody.org'">
</form>
</body>
</html>

这是 Javascript.js 中的 javascript 代码:

function weekPrice()
{
    var weekPrice=0;
    var theForm = document.forms["paymentform"];
    var includeWeek1 = theForm.elements["includeweek1"];
    var includeWeek2 = theForm.elements["includeweek2"];
    var includeWeek3 = theForm.elements["includeweek3"];
    var includeWeek4 = theForm.elements["includeweek4"];
    var includeWeek5 = theForm.elements["includeweek5"];
    var includeWeek6 = theForm.elements["includeweek6"];
    var afterCampCare = theForm.elements["aftercampcare"];


    if(includeWeek1.checked==true)
    {
        weekPrice= weekPrice + 75;
    }
    if (includeWeek2.checked==true)
    {
        weekPrice= weekPrice + 75;
    }
    if (includeWeek3.checked==true)
    {
        weekPrice= weekPrice + 75;
    }
    if (includeWeek4.checked==true)
    {
        weekPrice= weekPrice + 75;
    }
    if (includeWeek5.checked==true)
    {
        weekPrice= weekPrice + 60;
    }
    if (includeWeek6.checked==true)
    {
        weekPrice= weekPrice + 75;
    }
    if (afterCampCare.checked==true)
    {
        weekPrice= weekPrice + 2;
    }
    return weekPrice;
}
function getTotal()
{
var weekTotalPrice = weekPrice(); 
document.getElementsByClassName("totalPrice")[0].innerHTML = "" + weekTotalPrice;
}

这是我的问题:在下一页上,我再次需要 totalPrice 的值,并且需要将其输出到 html 上。在那之后的页面上,我需要将相同的值存储到数据库中。那么,如何使用 php 存储 Javascript 函数值,从而使用各种 php 函数呢?

4

3 回答 3

0

这里有提示:一旦提交,该值可以存储为 PHP 中的 $_SESSION 值。

于 2013-07-02T22:46:23.577 回答
0

在 HTML 页面上,在<form>and</form>标记之间添加以下内容:

<input type="hidden" name="totalprice" />

在您的 JavaScript 中,在getTotal()函数内部,添加以下行(在设置 weekTotalPrice 之后):

document.querySelector('input[type=hidden][name=totalprice]').value = weekTotalPrice;

然后,当您提交表单时,该字段也会发送过来!

您可以通过执行以下操作使用 PHP 在下一页上访问它:

<?php echo($_POST['totalprice']); ?>

您可以通过执行以下操作将其添加到另一个隐藏字段:

<input type="hidden" name="totalprice" value="<?php echo($_POST['totalprice']); ?>" />

于 2013-07-02T22:47:41.743 回答
0

为什么不直接使用 JavaScript 函数将总值设置为第一页表单上的隐藏输入字段值,然后在下一页检索 POST 的值并将其传递到另一个隐藏字段,然后您可以使用将值存储到数据库中。

于 2013-07-02T22:50:23.560 回答