代码之家  ›  专栏  ›  技术社区  ›  shinjuo

发送javascript变量

  •  0
  • shinjuo  · 技术社区  · 15 年前

    我有一个页面,允许用户在表单中选择一些内容,它将使用javascript计算权重。它把它分成5个变量,我需要发送到另一个页面。最初我只是让它把变量放到一个文本框中,然后我发布了那个文本框。但是我不想有5个文本框。所以现在我需要以某种方式将这五个变量发送或发布到另一个页面。这是我的javascript函数。我要寄一号重箱-五号重箱

    JS函数

    function getWeight(){
          var weightBoxOne;
          var weightBoxTwo;
          var totalWeight;
          var box = .5;
       var quantity = document.dhform.quantity.value;
       var cardSize = document.dhform.cardSize.value;
       if(cardSize == 0.0141){
        if(quantity <= 1000){
         weightBoxOne = (quantity * cardSize) + box;
         totalWeight = weightBoxOne;
        }else if(quantity > 1000 && quantity <= 2000){
         weightBoxOne = (1000 * cardSize) + box;
         weightBoxTwo = ((quantity - 1000) * cardSize) + box;
         totalWeight = weightBoxOne + weightBoxTwo;
        }else if(quantity > 2000 && quantity <= 3000){
         weightBoxOne = (1000 * cardSize) + box;
         weightBoxTwo = (1000 * cardSize) + box;
         weightBoxThree = ((quantity - 2000) * cardSize) + box;
         totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree; 
        }else if(quantity > 3000 && quantity <= 4000){
         weightBoxOne = (1000 * cardSize) + box;
         weightBoxTwo = (1000 * cardSize) + box;
         weightBoxThree = (1000 * cardSize) + box;
         weightBoxFour = ((quantity - 3000) * cardSize) + box;
         totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree + weightBoxFour;
        }else{
         weightBoxOne = (1000 * cardSize) + box;
         weightBoxTwo = (1000 * cardSize) + box;
         weightBoxThree = (1000 * cardSize) + box;
         weightBoxFour = (1000 * cardSize) + box;
         weightBoxFive = ((quantity - 4000) * cardSize) + box;
         totalWeight = weightBoxOne + weightBoxTwo + weightBoxThree + weightBoxFour + weightBoxFive;
        }
       }else if(cardSize == 0.00949){
        if(quantity <= 4000){
         weightBoxOne = (quantity * cardSize) + box;
         totalWeight = weightBoxOne;
        }else{
         weightBoxOne = (4000 * cardSize) + box;
         weightBoxTwo = ((quantity - 4000) * cardSize) + box;
         totalWeight = weightBoxOne + weightBoxTwo;
        }
       }
       document.rates.weight.value = totalWeight;
      }
      //-->
    

    这是最初发布的表单

    <form action="getRates.php" name="rates" method="post" onSubmit="popupform(this, 'join')">
                              <table style="width: 216px">
                                <tr>
                                  <td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
                                      <input type="text" id="weight" name="weight" size="10" maxlength="4"/>
                                  </td>
                                  <td align="right" style="width: 68px; height: 49px;" valign="top"><span class="style16">Zip Code</span><br/>
                                      <input type="text" id="zip" name="zip" size="10" maxlength="5"/>
                                  </td>
                                </tr>
                                <tr>
                                  <td style="width: 115px">
             <input name="submit" type="submit" value="Get Rate Costs" style="width: 138px" />
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   EAMann    15 年前

    我也做了类似的事情,将javascript变量传递到隐藏字段中,然后像往常一样将它们发布到表单中。

    只需在表单中添加一些隐藏字段,标记如下:

    <input type="hidden" id="hidden-field-1" value="">

    然后,您可以调整隐藏字段的值以匹配您的javascript变量,并将该值传递到下一页:

    document.rates.hidden-field-1.value = totalWeight;
    

    请记住,这种形式的javascript(我直接从您的帖子中获取)只能在Internet Explorer中可靠地工作…更好的方法是使用:

    document.forms['rates'].hidden-field-1.value = totalWeight;
    
        2
  •  0
  •   Jason S    15 年前

    忽略了这样一个事实,即您可以利用少量变量对其进行黑客攻击,并假设您同时控制客户机javascript和服务器上的接收页面。

    1. 当应该使用数组时,使用数组而不是不同的变量
    2. 使用JSON将数组序列化为字符串
    3. 将字符串发布到服务器上的接收页的表单字段中(隐藏或其他)
    4. 将JSON字符串反序列化为数组
    5. 在服务器上使用它