我是否将 Javascript 用于 HTML

发布于 2024-12-04 13:15:16 字数 1004 浏览 1 评论 0原文

我目前正在制作贝宝结帐表格。该表单允许用户指定一些选项,当他们提交表单时,它会将订单提交给 PayPal 进行处理。

该表单有一个

<select name="srt">                         <!-- User specifies amount of times payment should recur -->
    <option value="1"></option>
    <option value="2"></option>  
    <option value="3"></option>  
</select>
<input type="hidden" name="src" value="1">   <!-- Specifies that payment should be recurring-->

这在大多数情况下都有效,但是当用户使用 srt value="1" 选择第一个选项时,paypal 相反希望这是提交时 srt 为空或省略,src(表单下方的隐藏输入)为 value="0"。这样做的原因似乎是,如果付款重复 1 次,即仅向客户收取一次费用,那么这根本不是定期付款,因此 srt 无效,而是 < code>src(指定是否应重复付款)应设置为 0

我想知道处理这个问题的最佳方法是什么。我唯一能想到的是使用 javascript 根据用户在

这是处理这个问题的最佳方法,还是有更好的方法不需要浏览器处理 javascript?

谢谢!

I'm currently working on a paypal checkout form. The form allows the user to specify a few options and when they submit the form, it submits the order to paypal for processing.

The form has a <select> droplist that allows a user to select how many times they want a subscription to recur. It is set up like this:

<select name="srt">                         <!-- User specifies amount of times payment should recur -->
    <option value="1"></option>
    <option value="2"></option>  
    <option value="3"></option>  
</select>
<input type="hidden" name="src" value="1">   <!-- Specifies that payment should be recurring-->

This works in most cases, however when the user selects the first option with the srt value="1", paypal instead wants this to be submitted with srt blank or omitted and src (the hidden input below the form) with value="0". The reason for this seems to be that if the payment is to recur 1 time, meaning to charge the customer only once, then this is not a recurring payment at all, so srt is not valid and instead src, which specifies if payments should recur or not, should be set to 0.

I'm wondering what is the best way to handle this. The only thing I can think of is to use javascript to set hidden form values depending on what the user selects in a <select> drop list.

Is that the best way to handle this, or is there a better way that does not require the browser to handle javascript?

Thanks!

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

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

发布评论

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

评论(1

¢蛋碎的人ぎ生 2024-12-11 13:15:16

首先,我想澄清的是,在浏览器中计算的所有值必须在服务器上进行验证。服务器端验证确实无可替代。客户端脚本只能用于改善用户体验。 如果不这样做,就会出现一个等待被利用的安全漏洞。

  1. 要解决当前的问题,您可以利用发布表单时不会提交禁用的输入这一事实。因此,如果用户不选择定期付款,srt 将永远不会被发布。
  2. 首先将 srt 禁用并隐藏。
  3. 隐藏输入 src 的初始值应为 0
  4. 添加“定期付款?”复选框到表单。
  5. 如果用户选择该复选框,则通过 Javascript 启用并显示 srt
  6. src 的值设置为等于用户在 srt 中选择的值。
  7. 如果用户取消选中该复选框,则禁用并清空 srtsrc

DEMO

标记:

<p><input type="checkbox" value="recurring" id="recurring"/>Recurring Payment?</p>
<select disabled id="srt" name="srt">                         
    <option selected value="2">2</option>  
    <option value="3">3</option>  
</select>
<input type="hidden" id="src" name="src" value="0"> 

jQuery:

$('#recurring').click(function() {
    var $srt = $('#srt'),
        $src = $('#src');
    if (this.checked) {
        $srt.prop('disabled', false).toggle();
        $src.val($srt.val());
    } else {
        $srt.prop('disabled', true).toggle();
        $src.val(0);
    }
});

$('#srt').change(function() {
    $('#src').val($(this).val());
});

First of all I would like to make it clear that all values computed in the browser must be validated on the server. There really is no substitute for server side validation. Client-side scripts must only be used to improve user experience. Failing to do so is a security hole waiting to be exploited.

  1. To solve the issue at hand you could exploit the fact that disabled inputs aren't submitted when a form is posted. Thus if the user doesn't select to make a recurring payment srt will never get posted.
  2. Start out with srt both disabled and hidden.
  3. The hidden input src should have a value of 0 to start with.
  4. Add a "Recurring Payment?" checkbox to the form.
  5. If the user selects the checkbox, enable and show srt via Javascript.
  6. Set the value of src equal to whatever the user selects in srt.
  7. Disable and blank out srt and src if the user ever unchecks the checkbox.

DEMO

Markup:

<p><input type="checkbox" value="recurring" id="recurring"/>Recurring Payment?</p>
<select disabled id="srt" name="srt">                         
    <option selected value="2">2</option>  
    <option value="3">3</option>  
</select>
<input type="hidden" id="src" name="src" value="0"> 

jQuery:

$('#recurring').click(function() {
    var $srt = $('#srt'),
        $src = $('#src');
    if (this.checked) {
        $srt.prop('disabled', false).toggle();
        $src.val($srt.val());
    } else {
        $srt.prop('disabled', true).toggle();
        $src.val(0);
    }
});

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