将 JavaScript 变量传递给 Paypal

发布于 2024-12-09 17:08:18 字数 4628 浏览 1 评论 0 原文

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

下面是代码。

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

我很欣赏 Paypal 的安全问题……但除此之外,是否有人比我有更大的大脑,可以找到明显的解决方案。

谢谢!!

Andy

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="[email protected]">
<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”中的值获取到传递给贝宝的“金额”中。我不关心如何,但似乎该值是用“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="[email protected]">
<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>

I am trying to pass a variable amount (this being the total order value) to the 'amount' variable in a paypal form. However, the value is generated from a drop down list. I've scoured the forums and tried a few things, but can't find what I thought would be a simple solution.

Below is the code.

As you can see there are a couple of options I have tried, but neither pass the variable selected from the drop down list.

I appreciate the issues with Paypal security... but that aside, is there anyone with a much larger brain than mine who can spot an obvious solution.

Thanks!!

Andy

PS If I change the var 'andy' to, say, 33, it all works fine.

<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="[email protected]">
<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>

UPDATE: Thanks for the input from sdleihssirhc. That does help, but as I am a bit of a duffer with javascript am still struggling.

The bit I didn't emphasise is that I am trying to get the value from 'mytext' into 'amount' which is passed to paypal. And I dont care how, but it seems that as the value is calculated with an 'OnChange'....it all goes a bit bonkers.

The main bits of code now read as below on last bit of post.

ANY BRIGHT IDEAS ANYONE. MANY THANKS!!!!

<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="[email protected]">
<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-12-16 17:08:18

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

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

...但是 ID 为“finalpaypal”的元素还不存在。您能否将

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

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>

I think the first problem is with the your HTML - JavaScript - HTML ordering. You have this line in your JS...

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

...But the element with the ID "finalpaypal" doesn't exist, yet. Can you move the <script> to the bottom of the page, after all of the form's HTML?

The second problem is also with that line. You're trying to set the input's value to a variable that (as far as I can tell) doesn't actually exist. If you want the value to be the same as what you set the textbox to, just do this:

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

But then we get to the last problem: You should put this line inside the event handler.

So the final code will look like this:

<!-- 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>
烟燃烟灭 2024-12-16 17:08:18

为了使用 select 标签,您必须将其包含在您的 paypal 按钮表单中。您必须使用 on0os0 标记。

您可以在这里阅读更多相关信息:单击此处

您可以在此处查看示例:点击此处

href="https://developer.paypal.com/webapps/developer/docs/classic/paypal- payments- standard/integration-guide/paypal_shopping_cart/#id08AEC03L05H jsfiddle 示例就在这里

<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="[email protected]">
<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">

In order to use the select tag you have to include it in your paypal button form. You have to use the on0 and os0 tags.

You can read more about it here: click here

and you can see examples here: click here

and a jsfiddle example for you 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="[email protected]">
<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">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文