【问题标题】:Passing javascript variable to Paypal将 javascript 变量传递给 Paypal
【发布时间】:2023-03-26 10:55:01
【问题描述】:

我正在尝试将可变金额(这是总订单价值)传递给贝宝表单中的“金额”变量。但是,该值是从下拉列表中生成的。我搜索了论坛并尝试了一些方法,但找不到我认为的简单解决方案。

下面是代码。

如您所见,我尝试了几个选项,但都没有传递从下拉列表中选择的变量。

我很欣赏 Paypal 的安全问题……但除此之外,还有比我脑洞大得多的人能找到明显的解决方案吗?

谢谢!!

安迪

PS 如果我将 var 'andy' 更改为 33,则一切正常。

<label for="CurrentExcess">Current Excess (£100 - £500 Max.)</label>

<select id="dropdown"> 
  <option value="invalid entry">please select</option>
  <option value="12">£100</option>
  <option value="18">£150</option>
  <option value="24">£200</option>
  <option value="30">£250</option>
  <option value="36">£300</option>
  <option value="42">£350</option>
  <option value="48">£400</option>
  <option value="54">£450</option>
  <option value="60">£500</option>
  <option value="all us to discuss">more then £500</option>
</select> 


        </div>
        <div>
<label for="price">Your membership fee will be: £</label>
<input type="text" name="price" value="" id="mytext" />

<script type="text/javascript">
var mytextbox = document.getElementById('mytext'); 
var mydropdown = document.getElementById('dropdown'); 
mydropdown.onchange = function(){ 
      mytextbox.value = this.value; 
} 
document.getElementById("finalpaypal").value = mytext;
var andy = mytext
</script>


<form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="me@me.com">
<input type="hidden" name="item_name" value="My Title">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="item_number" value="11">
<!-- <script> 
document.write('<input type="hidden" name="amount" value='+andy+'>'); 
</script> -->
<input type="hidden" name="amount" id="finalpaypal">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="add" value="1">

</form>



</body>
</html>

更新:感谢 sdleihssirhc 的意见。这确实有帮助,但由于我对 javascript 有点笨拙,我仍在苦苦挣扎。

我没有强调的一点是,我试图将“mytext”中的值转换为“amount”,然后传递给paypal。而且我不在乎如何,但似乎该值是使用“OnChange”计算的......这一切都变得有点疯狂。

代码的主要部分现在在帖子的最后一点上如下所示。

任何人都有任何好主意。非常感谢!!!!

<label for="CurrentExcess">Current Excess (£100 - £500 Max.)</label>

<select id="dropdown"> 
  <option value="invalid entry">please select</option>
  <option value="12">£100</option>
  <option value="18">£150</option>
  <option value="24">£200</option>
  <option value="30">£250</option>
  <option value="36">£300</option>
  <option value="42">£350</option>
  <option value="48">£400</option>
  <option value="54">£450</option>
  <option value="60">£500</option>
  <option value="all us to discuss">more then £500</option>
</select> 


        </div>
        <div>
<label for="price">Your membership fee will be: £</label>
<input type="text" name="price" value="" id="mytext" />
<form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="me@me.com">
<input type="hidden" name="item_name" value="My Title">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="item_number" value="11">
<input type="hidden" name="amount" id="finalpaypal">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="add" value="1">

</form>

<script> 
var mytextbox = document.getElementById('mytext');  
var mydropdown = document.getElementById('dropdown');  
var finalpaypal = document.getElementById('finalpaypal'); 
mydropdown.onchange = function(){  
mytextbox.value = this.value; 
finalpaypal.value = this.value; 
}  
document.getElementById("finalpaypal").value = mytext; 
</script>

</body>
</html>

【问题讨论】:

    标签: javascript variables paypal parameter-passing


    【解决方案1】:

    为了使用select 标签,您必须将它包含在您的贝宝按钮表单中。您必须使用on0os0 标签。

    您可以在此处阅读更多信息:click here

    您可以在此处查看示例:click here

    还有一个 jsfiddle 例子给你right here

    <div>
    <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="business" value="me@me.com">
    <input type="hidden" name="item_name" value="My Title">
    <input type="hidden" name="quantity" value="1">
    <input type="hidden" name="item_number" value="11">
    <input type="hidden" name="no_note" value="1">
    <input type="hidden" name="currency_code" value="GBP">
    <input type="hidden" name="add" value="1">
    <!-- Here are your select options-->
    <input type="hidden" name="on0" value="prices">
    <select name="os0"> 
    <option value="please select">please select</option>
    <option value="12">£100</option>
    <option value="18">£150</option>
    <option value="24">£200</option>
    <option value="30">£250</option>
    <option value="36">£300</option>
    <option value="42">£350</option>
    <option value="48">£400</option>
    <option value="54">£450</option>
    <option value="60">£500</option>
    </select>  
    <!-- This is how paypal will get the prices from your options menu-->
    <input type="hidden" name="option_index" value="1">
    <input type="hidden" name="option_select0" value="12">
    <input type="hidden" name="option_amount0" value="100">
    <input type="hidden" name="option_select1" value="18">
    <input type="hidden" name="option_amount1" value="150">
    <input type="hidden" name="option_select2" value="24">
    <input type="hidden" name="option_amount2" value="200">
    <!--etc...-->
    
    <!-- Here is the button -->
    <input type="submit" value="Buy Now">
    

    【讨论】:

      【解决方案2】:

      我认为第一个问题是您的 HTML - JavaScript - HTML 排序。您的 JS 中有这一行...

      document.getElementById("finalpaypal").value = mytext.value;
      

      ...但是 ID 为“finalpaypal”的元素还不存在。在所有表单的 HTML 之后,您能否将 &lt;script&gt; 移动到页面底部?

      第二个问题也与那条线有关。您正在尝试将输入的值设置为(据我所知)实际上并不存在的变量。如果您希望该值与您设置文本框的值相同,只需执行以下操作:

      document.getElementById("finalpaypal").value = mytextbox.value;
      

      但是我们遇到了最后一个问题:您应该将这一行放在事件处理程序中。

      所以最终的代码将如下所示:

      <!-- all of the form's HTML -->
      
      <script>
      var mytextbox = document.getElementById('mytext'); 
      var mydropdown = document.getElementById('dropdown'); 
      var finalpaypal = document.getElementById('finalpaypal');
      mydropdown.onchange = function(){ 
          mytextbox.value = this.value;
          finalpaypal.value = this.value;
      } 
      document.getElementById("finalpaypal").value = mytext;
      </script>
      

      【讨论】:

      • 对不起,我点击了错误的编辑,它认为这是我的帖子
      • 我已经更新了我的第一篇文章。任何对如何使用 ONCHANGE 命令传递从下拉列表中选择的变量有一个好主意的人 - 传递到 PayPal 提交的“金额”区域……我将永远感激不尽!!!跨度>
      猜你喜欢
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2018-09-23
      • 2012-01-28
      • 2012-10-24
      • 2021-04-21
      • 2019-03-24
      • 2021-04-16
      相关资源
      最近更新 更多