简体   繁体   中英

Update a JS array value with a global variable based on checkbox (true/false) form input

I'm using a WorldPay JS function to create a payment form. This function creates a TOKEN that can be reusable or not. I need to update the 'reusable' flag based on a form input (checkbox) but I can't get the global variable (reuse) to update. I've created a function CHECKED that updates the variable but the WorldPay JS just ignores it. I think is due the window.onload status, but I don't know how to fix it. Any help would be greatly appreciated.

 <?php
include('./header.php');
require_once('./init.php');
?>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8" />
        <script src="https://cdn.worldpay.com/v1/worldpay.js"></script>    
        <script type='text/javascript'>
            var reuse = false;
            function Checked(){
                reuse = document.getElementById('check').checked;
                Worldpay.submitTemplateForm();
            }

            window.onload = function() {
                Worldpay.useTemplateForm({
                'clientKey':'ENTER CLIENT KEY',
                'form':'paymentForm',
                'paymentSection':'paymentSection',
                'display':'inline',
                'type':'card',
                'reusable': reuse,
                'saveButton':false,
                'callback':function(obj){
                    if (obj && obj.token && obj.paymentMethod) {
                    var _el = document.createElement('input');
                    _el.value = obj.token;
                    _el.type = 'hidden';
                    _el.name = 'token';
                    document.getElementById('paymentForm').appendChild(_el);
                    var _name = document.createElement('input');
                    _name.value = obj.paymentMethod.name;
                    _name.type = 'hidden';
                    _name.name = 'customer';
                    document.getElementById('paymentForm').appendChild(_name);
                    document.getElementById('paymentForm').submit();

                    }

                }
                });
                }
        </script>

    </head>
    <body>
        <form action="./test.php" id="paymentForm" method="post">
            <!-- all other fields you want to collect, e.g. name and shipping address -->
            <div id='paymentSection'></div>
            <div>
            <input type="checkbox" id='check'>
            <input type="submit" value="Place Order" onclick="Checked()" />
            </div>

        </form>
    </body>
</html>

NOTE: I've removed the client ID so the code won't run.

Have you tried to use function "checked" on window.onload like this:

  window.onload = function() {
            Worldpay.useTemplateForm({
           //code....
          )}
 function Checked(){
    //code....
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM