我对Jquery AjAX很新,所以我从jquery UI中找到了这个模态形式,遗憾的是代码只是将表附加到现有的html文档中,我想要的是能够将这些变量传递给php所以我可以将它们用于我的数据库这是模式形式的链接 - » jquery ui form
无论如何,我将展示我的代码:
var firstname = $( "#firstname" ),
lastname = $ ( "#lastname" ),
email = $( "#email" ),
nickname = $( "#nickname" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ).add( nickname ),
tips = $( ".validateTips" );
$( "#create-form" ).dialog({
autoOpen: false,
width: 350,
modal: true,
show: "fold",
hide: "fade",
buttons: {
"Create an account": function() {
var bValid = true;
bValid = bValid && checkLength( firstname, "firstname", 3, 16 );
bValid = bValid && checkLength( lastname, "lastname", 3, 16 );
bValid = bValid && checkLength( email, "email", 6, 80 );
bValid = bValid && checkLength( nickname, "nickname", 3, 16 );
bValid = bValid && checkLength( password, "password", 5, 16 );
bValid = bValid && checkRegexp( firstname, /^[a-z ]+$/i, "First Name may consist of a-z" );
bValid = bValid && checkRegexp( lastname, /^[a-z ]+$/i, "Last Name may consist of a-z" );
bValid = bValid && checkRegexp( nickname, /^[a-z]([0-9a-z])+$/i, "Username may consist of a-z, 0-9, no underscores." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( bValid ) {
以下所有内容未触及我添加的代码:
$.ajax({
url:'submit.php',
type:'GET',
data: { fname: firstname, lname: lastname },
success:function(data){
//$("#response").text(result);
alert("success!");
}
});
$( "#users tbody" ).append( "<tr>" +
"<td>" + firstname.val() + lastname.val()+ "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"<td>" + nickname.val() +"</td>" +
"</tr>" );
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
你能帮我吗?
答案 0 :(得分:1)
根据您对超出最大堆栈大小错误的评论,我相信您的验证可能会导致该问题。让我们尝试通过以下评论来禁用它们:
//bValid = bValid && checkLength( firstname, "firstname", 3, 16 );
//bValid = bValid && checkLength( lastname, "lastname", 3, 16 );
//bValid = bValid && checkLength( email, "email", 6, 80 );
//bValid = bValid && checkLength( nickname, "nickname", 3, 16 );
//bValid = bValid && checkLength( password, "password", 5, 16 );
//bValid = bValid && checkRegexp( firstname, /^[a-z ]+$/i, "First Name may consist of a-z" );
//bValid = bValid && checkRegexp( lastname, /^[a-z ]+$/i, "Last Name may consist of a-z" );
//bValid = bValid && checkRegexp( nickname, /^[a-z]([0-9a-z])+$/i, "Username may consist of a-z, 0-9, no underscores." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
//bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
//bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
让执行流程并执行ajax调用。如果你从你的php服务器得到一个响应,那么问题在于验证。
然后问题必须是你将DOM对象作为get参数传递,并且jquery可能试图序列化超过其能够处理的数量,所以让我们试试这个:
取代:
type:'GET',
data: { fname: firstname, lname: lastname }
使用:
type:'POST',
data: { fname: firstname.val(), lname: lastname.val() }
答案 1 :(得分:1)
您需要传递输入元素的值:
data : { fname: firstname.val(), lname: lastname.val() }
我会说你还需要对它们进行编码:
data : { fname: encodeURIComponent(firstname.val()),
lname: encodeURIComponent(lastname.val())
}
您还应该将方法更改为POST
而不是GET
。应使用GET
方法获取数据,而不是发布数据。还有一些差异,比如POST没有缓存,POST没有长度限制(理论上),POST没有添加到URL ¶m¶m
等等。
只需更改为
$.ajax({
type: 'POST',
...
现在在submit.php
中,您可以将值定位为
$firstname = urldecode($_POST['fname']);
$lastname = urldecode($_POST['lname']);
从这里开始,将其余值添加到data : {}
并在submit.php
中使用它们不应该有任何问题。
答案 2 :(得分:0)
Ajax可以做到这一点。谷歌吧,查看api.jquery.com并查看ajax函数,.ajax(),. post(),. get(),。load()等。来源:ExplosionPills
这是你要做的:
//Javascript file
$("input[type=checkbox]").click(function () {
$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) {
alert(response);
});
});
//PHP file my_ajax_receiver.php
<?php
$value = $_POST['val'];
echo "I got your value! $value";
?>