jQuery CodeIgniter/Ajax - 将 post 值发送到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5529609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter/Ajax - Send post values to controller
提问by cabaret
I'm working on a simple login form. When the user clicks on the Login button, I want to send the post values to my controller, validate them against my database (using a model) and return a value. Based on that value, I want to either send the user to the member area, or display an error message on my form. This error message has a class 'error' and is hidden by default. I want to use jQuery's show() to show it when the credentials are wrong.
我正在处理一个简单的登录表单。当用户单击登录按钮时,我想将帖子值发送到我的控制器,根据我的数据库(使用模型)验证它们并返回一个值。根据该值,我想将用户发送到会员区,或者在我的表单上显示错误消息。此错误消息有一个“错误”类,默认情况下是隐藏的。当凭据错误时,我想使用 jQuery 的 show() 来显示它。
My form has two textfields, one for the email address, other one for the password and a submit button. However, I have never really worked with Ajax except for VERY basic things like a simple loader, so I'm not sure what to do next.
我的表单有两个文本字段,一个用于电子邮件地址,另一个用于密码和一个提交按钮。但是,我从来没有真正使用过 Ajax,除了非常基本的东西,比如一个简单的加载器,所以我不确定接下来要做什么。
$("#btn_login").click(
function()
{
// get values
var email_address = $("#email_address").val();
var password = $("#password").val();
// post values? and ajax call?
//stop submit btn from submitting
return(false);
}
);
I assume I have to use jQuery's ajax() method here, so I was thinking of working off this example:
我假设我必须在这里使用 jQuery 的 ajax() 方法,所以我想解决这个例子:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
However, I'm not really sure how to get my post values (those two vars) in the data: thingy.. Or should I take another road here? This seems like something I'll never forget how to do once I learn it, so if someone can help me out I'd be grateful. Thanks a lot.
但是,我不太确定如何在数据中获取我的帖子值(这两个变量):thingy .. 或者我应该在这里走另一条路?这似乎是我一旦学会了就永远不会忘记怎么做的事情,所以如果有人能帮助我,我将不胜感激。非常感谢。
回答by JohnP
All you need to do is create a key/value pair with the values and assign it to the data parameter and jQuery will do the rest.
您需要做的就是使用值创建一个键/值对并将其分配给数据参数,jQuery 将完成剩下的工作。
//create a js object with key values for your post data
var postData = {
'email' : email_address,
'password' : password
};
$.ajax({
type: "POST",
url: "some.php",
data: postData , //assign the var here
success: function(msg){
alert( "Data Saved: " + msg );
}
});
With this, you should be able to access the data with Codeigniters input
有了这个,您应该能够使用 Codeigniters 访问数据 input
EDIT
编辑
I've set up a test fiddle here : http://jsfiddle.net/WVpwy/2/
我在这里设置了一个测试小提琴:http: //jsfiddle.net/WVpwy/2/
$('#dostuff').click(function(){
var email_address = $("#email_address").val();
var password = $("#password").val();
var postData = {
'email' : email_address,
'password' : password,
'html' : 'PASS'
};
$.post('/echo/html/', postData, function(data){
//This should be JSON preferably. but can't get it to work on jsfiddle
//Depending on what your controller returns you can change the action
if (data == 'PASS') {
alert('login pased');
} else {
alert('login failed');
}
});
});
I just prefer .post, but what you used is an equivalent.
我只是更喜欢 .post,但你使用的是等效的。
Basically your controller should echoout data. Not return. You need to send a string representation of your data back so your script can (evaluate if json) and act on it
基本上你的控制器应该回显数据。不回。您需要将数据的字符串表示发送回,以便您的脚本可以(评估是否为 json)并对其进行操作
Here's a good example as a starting point : http://www.ibm.com/developerworks/opensource/library/os-php-jquery-ajax/index.html
这是一个很好的例子作为起点:http: //www.ibm.com/developerworks/opensource/library/os-php-jquery-ajax/index.html
回答by user1484668
just modifiy a little bit the url of your ajax :
只需稍微修改一下您的 ajax 网址:
$.ajax({
type: "POST",
url: "some/myfunction",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Make sure your url point to the function you want inside your controller. For example: "myfunction" inside the controller some (in the file Some.php)
确保您的 url 指向您想要在控制器中使用的功能。例如:控制器中的“myfunction” some(在文件 Some.php 中)
To access to your post variables inside the controller function do not try to put parameters to myfunction but :
要访问控制器函数内的 post 变量,请不要尝试将参数放入 myfunction,而是:
class Some extends CI_Controller
{
......
public function myfunction()
{
$name = $this->input->post('name');
$location = $this->input->post('location');
}
......
}
回答by Harold Smith
You can definitely use your method, but a shorthand method is this:
您绝对可以使用您的方法,但速记方法是这样的:
$.post("some.php",{name:"John",location:"Boston",email:email_address,password:password},function(data,textStatus) { //response from some.php is now contained in data });