I have a form that I am trying to submit inside of fancybox (www.fancybox.net). If I go directly to the form it will submit fine. However, if I load the form inside of the page I want to call fancybox from the form will not submit and fancybox goes away and the page refreshes. I have tried both fancybox and facebox to same issue.. below is the Jquery code I have all in the main page. This page opens fancybox and subsquently the form for submission. The form is supposed to show a thank you message from in the window when it submits instead of refreshing the page.. Any help is much appreciated:
JavaScript:
$(document).ready(function () {
$("#various1").fancybox({
'opacity': true,
'overlayShow': false,
'transitionIn': 'none',
'autoscale': 'false',
'transitionOut': 'none'
});
$("submit").submit(function () {
alert('it submits');
// we want to store the values from the form input box, then send via ajax below
var name = $('#name').attr('value');
var email = $('#email').attr('value');
var password = $('#password').attr('value');
var custom1 = $('#custom1').attr('value');
var custom2 = $('#custom2').attr('value');
var custom3 = $('#custom3').attr('value');
var custom4 = $('#custom4').attr('value');
var custom5 = $('#custom5').attr('value');
var custom6 = $('#custom6').attr('value');
var custom7 = $('#custom7').attr('value');
var custom8 = $('#custom8').attr('value');
var custom9 = $('#custom9').attr('value');
var custom10 = $('#custom10').attr('value');
$.ajax({
type: "POST",
url: "test.php",
data: "name=" + name + "& email=" + email + "& password=" + password + "& custom1=" + custom1 + "& custom2=" + custom2 + "& custom3=" + custom3 + "& custom4=" + custom4 + "& custom5=" + custom5 + "& custom6=" + custom6 + "& custom7=" + custom7 + "& custom8=" + custom8 + "& custom9=" + custom9 + "& custom10=" + custom10,
success: function () {
$('submit').fadeOut(function () {
$('div.success').fadeIn();
});
}
});
return false;
})
; });
Link to form:
<a id="various1" href="register.php">
data: "name=" + name + "& email=" + email + "& password=" + password + "& custom1=" + custom1 + "& custom2=" + custom2 + "& custom3=" + custom3 + "& custom4=" + custom4 + "& custom5=" + custom5 + "& custom6=" + custom6 + "& custom7=" + custom7 + "& custom8=" + custom8 + "& custom9=" + custom9 + "& custom10=" + custom10,
That's so wrong, incorrect and ugly. There shouldn't be spaces after &
. Actually, passing a string there is already bad. Pass an object like this:
var data = { name: name, email: email, password: password, custom1: custom1, ... };
using data: data
Here's the fixed version:
$(document).ready(function () {
$("#various1").fancybox({
'opacity': true,
'overlayShow': false,
'transitionIn': 'none',
'autoscale': 'false',
'transitionOut': 'none'
});
$("form").submit(function () {
var data = {
name: $('#name').val(),
email: $('#email').val(),
password: $('#password').val(),
custom1: $('#custom1').val(),
custom2: $('#custom2').val(),
custom3: $('#custom3').val(),
custom4: $('#custom4').val(),
custom5: $('#custom5').val(),
custom6: $('#custom6').val(),
custom7: $('#custom7').val(),
custom8: $('#custom8').val(),
custom9: $('#custom9').val(),
custom10: $('#custom10').val()
};
$.ajax({
type: "POST",
url: "test.php",
data: data,
success: function() {
$('submit').fadeOut(function () {
$('div.success').fadeIn();
});
}
});
return false;
})
You should really use the jquery form plugin to submit that form though.. Saves you a lot of work typing all the field names...
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.