drsrq26482
2018-04-09 22:10
浏览 61
已采纳

Jquery Ajax获取提交表单上的当前URL

I used the code from https://scotch.io/tutorials/submitting-ajax-forms-with-jquery to post these data from these form

 <form id="request" method="post" autocomplete="off" class="form-horizontal row scrollTo" novalidate="novalidate">
    <div class="form-group row">
    <label class="control-label label-font" for="lastname">Lastname</label>
    <input name="lastname" id="lastname" class="form-control required" type="text">
    <label class="control-label label-font" for="firstname">Firstname</label>
    <input name="firstname" id="firstname" class="form-control required" type="text">
    </div>
    <div class="form-group row">
    <label class="control-label label-font" for="mobile">Mobile</label>
    <input name="mobile" id="mobile" class="form-control required number" type="text" minlength="10" maxlength="10">
    <label class="control-label label-font" for="vat">vat</label>
    <input name="vat" id="vat" class="form-control validVAT required" maxlength="9" type="text">
    </div>
    <div class="form-group row">
    <label class="control-label label-font" for="address">Address</label>
    <input name="address" id="address" class="form-control required" type="text">
    <label class="control-label label-font" for="streetno">Streetno</label>
    <input name="streetno" id="streetno" class="form-control required" type="text">
    </div>
    <div class="form-group row">
    <label class="control-label label-font" for="user_postcode">Postcode</label>
    <select name="user_postcode" id="user_postcode" class="form-control select2 required select2-hidden-accessible" tabindex="-1" aria-hidden="true"><option value="">Select</option><optgroup label="country1">
    <option value="3002123">3002123 Las Vegas</option><option value="3002723">3002723 Los Angeles</option></optgroup></select><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 100px;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-user_postcode-container"><span class="select2-selection__rendered" id="select2-user_postcode-container" title="3002123 Las Vegas">3002123 Las Vegas</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span> </div>
    </div>
    <label class="control-label label-font" for="user_email">Email</label>
    <input name="user_email" id="user_email" class="form-control required" type="text">
    </div>
    <div class="form-group row">
    <label class="control-label label-font" for="notes">Notes</label>
    <textarea type="text" name="notes" id="notes" class="form-control"></textarea>
    </div>
    <div class="form-group text-center">
    <input type="hidden" name="host"><input type="hidden" name="userID" class="userID" value="ho9fbhp3mhb"><input type="hidden" name="usage" value="auto">
    <button type="submit" class="btn btn-primary btn-sm" data-wizard="letters">Submit</button>
    </div>
    </form>

in .php file.

<script type="text/javascript">

            jQuery(document).ready(function() {

                jQuery("#request").submit(function(event) {

                    var formData = {
                        'lastname'         : jQuery('input[name=lastname]').val(),
                        'firstname'        : jQuery('input[name=firstname]').val(),
                        'phone'            : jQuery('input[name=mobile]').val(),
                        'afm'              : jQuery('input[name=vat]').val(),
                        'address'          : jQuery('input[name=address]').val(),
                        'address_no'       : jQuery('input[name=streetno]').val(),
                        'user_postcode'    : jQuery('select[name=user_postcode]').val(),
                        'email'            : jQuery('input[name=user_email]').val(),
                        'notes'            : jQuery('textarea[name=notes]').val()
                    };

                    jQuery.ajax({
                        type        : 'POST', 
                         url         : 'process.php', // the url where we want to POST 
                        data        : formData, 
                        dataType    : 'json', 
                                    encode          : true
                    })
                        .done(function(data) {

                            console.log(data); 

                        });

                    event.preventDefault();
                });

            });

        </script>

This is the code I wrote but I want to save in .php file also the current url. Which is the code to do this? Where and how sould I put the code? Also, could I set all this code only when the form is valid?

2条回答 默认 最新

相关推荐 更多相似问题