I have a form that's being validated using jQuery Validate and then submitted to a third-party subscription site.
I'm trying to get the submission posted elsewhere, too.
Here's my form:
<form class="" action="http://www.fakelink.com/forms/userSubmit.jsp" method="post" id="providerDemoForm" accept-charset="UTF-8">
<fieldset>
<ul class="undecorated group">
<li>
<label for="fld_1_fn">First Name*</label>
<input type="text" name="First Name" id="fld_1_fn" class="required" onFocus="clearMsg();" />
</li>
</fieldet>
</form>
Here's my validation script:
<script type="text/javascript">
function postCMFields() {
$.getJSON(
"http://sample.createsend.com/x/x/x/fmill/?callback=?",
$('#providerDemoForm').serialize()
);
}
$(document).ready(function() {
$("#providerDemoForm")[0].reset();
$("#providerDemoForm").validate({
errorClass: "fieldWithErrors",
validClass: "valid",
highlight: function(element, errorClass, validClass) {
$(element).parent("li").addClass(errorClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).parent("li").removeClass(errorClass);
},
errorContainer: "#formErrorMsg",
errorLabelContainer: "#messageBox",
wrapper: "li", debug:false,
submitHandler: function(form) {
$('#formErrorMsg').hide();
postCMFields();
form.submit();
},
invalidHandler: function(form, validator) {
$('#formErrorMsg').show();
}
});
});
</script>
Everything submits if I don't have that getJSON part (form submits the action url). If I add in the function to post the contents with JSON to my campaignmonitor url, though, it doesn't work. Furthermore, if I comment out "form.submit()", the data DOES get posted to campaignmonitor.开发者_StackOverflow中文版
Is there something I'm missing? Thanks!
I'm not certain why my code wasn't working, but I think it was because my MIME type wasn't set correctly. I was able to get things working by updating my postCMFields
function to use .ajax
:
var cmdata = $('#providerDemoForm').serialize();
$.ajax({
type: "GET",
data: cmdata,
url: "http://fake.createsend.com/x/x/x/fmill/?callback=?",
async: false,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
dataType: "json",
success: function(data){
form.submit();
}
});
The URL seems fictitious. http://sample.createsend.com/x/x/x/fmill/?callback=? looks like a get URL that you bind variables to.
精彩评论