通过AJAX向Google表格提交表单答案,然后重定向到paypal

时间:2018-01-20 20:54:33

标签: javascript jquery html ajax paypal

我有一个HTML表单,当按下提交按钮时,将表单数据插入到GoogleSheets电子表格中,然后显示一个模态,然后它应该重定向到PayPal,这就是我被卡住的地方。有很多教程/指南可以做到这一点,但我有一个单选按钮来选择产品,每个都有不同的价格,所以我试图提交表格,然后重定向到PayPal,但使用正确的产品和金额,所以选择一个不同的单选按钮将在结账时以不同的金额转到PayPal。

表格代码:

 var $form = $('form#bookingForm'),
    url = 'https://script.google.com/macros/s/AKfycbwaEsXX1iK8nNkkvL57WCYHJCtMAbXlfSpSn3rsJj2spRi-41Y/exec'

function disableAll() {
    $('#c-rail-yes').attr('required', false).attr('disabled', true);
    $('#c-rail-no').attr('required', false).attr('disabled', true);
    $('#craftName').attr('required', false).attr('disabled', true);
    $('#bizName').attr('required', false).attr('disabled', true);
    $('#insurance-yes').attr('required', false).attr('disabled', true);
    $('#insurance-no').attr('required', false).attr('disabled', true);
}

$('#stallType-Preloved').change(function () {
    if(this.checked) {
        disableAll();
        $('#c-rail-yes').attr('required', true).attr('disabled', false);
        $('#c-rail-no').attr('required', true).attr('disabled', false);
    }
});

$('#stallType-Craft').change(function () {
    if(this.checked) {
        disableAll();
        $('#craftName').attr('required', true).attr('disabled', false);
    }
});

$('#stallType-Business').change(function () {
    if(this.checked) {
        disableAll();
        $('#bizName').attr('required', true).attr('disabled', false);
        $('#insurance-yes').attr('required', true).attr('disabled', false);
        $('#insurance-no').attr('required', true).attr('disabled', false);
    }
});

$('#submit-form').on('click', function(e) {
    var valid = this.form.checkValidity();
    if (valid) {
        e.preventDefault();
        var jqxhr = $.ajax({
            url: url,
            method: "GET",
            dataType: "json",
            dataType: "json",
            data: $form.serializeObject(),
            success: function () {
                $('#redirect_page').modal('show');
                window.setTimeout(function () {
                    location.reload()
                }, 3000);
            }
        });
    }
});

HTML表单:

<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
    <title>MooWoos Stall Booking</title>
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <!--build:css css/styles.min.css-->
        <link rel="stylesheet" href="/css/bootstrap.css">
        <link rel="stylesheet" href="/css/style.css">
    <!--endbuild-->

</head>

<body>
<div class="container">
    <nav class="navbar navbar-toggleable-md navbar-light">
        <a class="logo"><img src="assets/logo_opt.png"></a>
    </nav>

    <hr>

    <div class="modal fade" id="redirect_page" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="form-horizontal">

                    <div class="modal-body">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <div id="user_msg" align="left">Booking successful! Redirecting to PayPal... </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-6 col-md-offset-4 bookingform">
            <h1>Stall Booking Form</h1>
            <p class="lead">
                Fill out the form to book and pay for your stall!
            </p>
            <form id="bookingForm" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"">
                <!-- BELOW ARE PAYPAL FIELDS FROM PAYPAL GENERATION -->
                <input type="hidden" name="cmd" value="_s-xclick">
                <input type="hidden" name="hosted_button_id" value="LGC5D5B6YVFF8">
                <input type="hidden" name="currency_code" value="GBP">
                <div class="form-group">
                    <label for="name">Name </label>
                    <input type="text" name="name" class="form-control" placeholder="Your Name" value="" title="Please enter your name" required/>
                </div>
                <div class="form-group">
                    <label for="address">Address </label>
                    <textarea name="address" class="form-control" placeholder="Your Address" value="" title="Please enter your address" required></textarea>
                </div>
                <div class="form-group">
                    <label for="phone">Telephone Number </label>
                    <input type="text" name="phone" class="form-control" placeholder="Your Telephone Number" value="" title="Please enter the best telephone number to contact you on" required/>
                </div>
                <div class="form-group">
                    <label for="email">Email </label>
                    <input type="text" name="email" class="form-control" placeholder="Your Email" value="" title="Please enter your Email address" required/>
                </div>
                <div class="form-group">
                    <label for="date">Which date would you like to book? </label>
                    <p><input type="radio" name="date" value="13th September" required/> Sunday 13th September</p>
                    <p><input type="radio" name="date" value="6th February" /> Saturday 6th February</p>
                </div>
                <div class="form-group">
                    <label>What type of stall do you require?</label>
                    <div>
                        <input type="radio" name="stallType" id="stallType-Preloved" value="Preloved" required>
                        <label for="stallType-Preloved">Preloved</label>
                        <div class="reveal-if-active">
                            <label for="c-rail">Will you be bringing a clothes rail? </label>
                            <input type="radio" id=c-rail-yes name="c-rail" value="Yes" /> Yes
                            <input type="radio" id=c-rail-no name="c-rail" value="No" /> No
                        </div>
                    </div>
                    <div>
                        <input type="radio" name="stallType" id="stallType-Craft" value="Craft">
                        <label for="stallType-Craft">Craft</label>
                        <div class="reveal-if-active">
                            <label for="craftName">What name do you use?</label>
                            <input type="text" id="craftName" name="craftName" class="require-if-active form-control" placeholder="Craft Name" title="Please provide us with your Craft name" value="" />
                        </div>
                    </div>
                    <div>
                        <input type="radio" name="stallType" id="stallType-Business" value="Business">
                        <label for="stallType-Business">Business</label>
                        <div class="reveal-if-active">
                                <label for="bizName">What is your business name?</label>
                                <input type="text" id="bizName" name="bizName" class="require-if-active form-control" placeholder="Business Name" title="Please provide us with your Business name" value="" />
                                <br>
                                <label for="insurance">Do you have Public Liability Insurance?</label>
                                <input type="radio" id="insurance-yes" name="insurance" class="require-if-active" data-require-pair="#stallType-Business" title="We will require proof of this prior to market day" value="Yes"/> Yes
                                <input type="radio" id="insurance-no" name="insurance" class="require-if-active" data-require-pair="#stallType-Business" title="Our insurance does not cover other businesses. Please ensure you have adequate cover and provide us with proof prior to market day" value="No"/> No
                        </div>
                    </div>
                </div>
                    <div>
                        <input type="submit" id="submit-form" class="btn btn-success btn-lg" value="Book & Pay" />
            </form> </div>
            <p></p>
        </div>

    </div>

    <hr>

    <footer>
        <div class="row">
            <div class="col-lg-12">
                <p>Copyright &copy; MooWoos 2018. Booking Form by Luke Brewerton</p>
            </div>
        </div>

    </footer>

</div>

<!--build:js js/mwbookings-min.js -->
<script src="js/jquery.min.js"></script>
<script src="js/tether.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.serialize-object.min.js"></script>
<script src="js/main.js"></script>
<!-- endbuild -->

</body>
</html>

我已经更新了上面的代码,并对表单的显示方式进行了一些其他更改。

我已经查看了PayPal网站上有关使用按钮等的文档,但这似乎只能使用DropDown而不是单选按钮。

我已经选择了单选按钮,因为我认为在这种情况下它们看起来更好,而且我还有额外的选项,这取决于PayPal中不需要的选项,而是我的GoogleSheets后端。

0 个答案:

没有答案