我如何使用jQuery表单插件发送我正在工作的表单的ajax版本作为一个变量,然后可以发送到数据库?我想拉几个输入(如订单名称和日期),并把它们放在数据库或组织的目的,但我希望能够存储整个订单表单的基础,以及,以便表单可以调用和编辑,如果需要以后。jQuery表单插件似乎非常适合这样做,但它将表单发送到文件目的地,我需要它被发送到一个变量,这样我就可以将它发送到数据库。如果有人对如何做到这一点有任何想法,将不胜感激!非常感谢!
如果你只是想发送你的表单到Laravel,你只需要jQuery做它:
jQuery('#saveButton').click(function(evnt) {
var href = $("#"+event.target.id).closest('form').attr('action');
var form = $("#"+event.target.id).closest('form').attr('id');
jQuery.post(href, jQuery("#"+form).serialize())
.done(function(data) {
if (data.success == "true") {
/// do what you need to do in case of success
} else {
/// do what you need to do in case of error
}
})
return false;
});
1)设置事件点击id为saveButton
的按钮
6)我的控制器应该返回一些data
,具有success
属性,我检查并在前端做任何我需要做的事情
在你的Laravel控制器中,你只需要:
Input::get('email');
Input::get('password');
你甚至可以这样做:
Post::create(Input::all());
注意,Input::all()
为您提供了一个字段数组,因此您可以删除不需要的字段。这只是一个例子,说明有很多方法可以在表中存储用户字段,这只是其中一个,非常简单的一个:
$post = Post::find(Input::get('id'));
$post->title = Input::get('title');
$post->body = Input::get('body');
$input = Input::all();
unset($input['title']);
unset($input['body']);
$post->userFields = json_encode($input);
$post->save();
正如您在评论中所指出的,您可以使用Input::except()和循环来获得所需的内容。
如果你需要在Form::model()
中使用这些字段,你可以把它们塞回你的模型:
$post = Post::find(1);
foreach(json_decode($post->userFields) as $key => $value)
{
$post->attributes[$key] = $value;
}
然后传递它:
Form::model($post...);