使用带有laravel的ajax提交表单时,避免页面重新加载

编程入门 行业动态 更新时间:2024-10-28 10:34:27
本文介绍了使用带有laravel的ajax提交表单时,避免页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我完全不想尝试使用ajax而不是laravel处理表单提交以避免页面重新加载等。但它不起作用,我不知道为什么。我通过示例搜索了精彩的网页,但似乎没有什么对我有用。这是我能得到的最接近的。我的知识有限,但我的抱负很高。请花一点时间查看我的代码,因为我现在处于精神崩溃的边缘。

I'm completely stuck trying to process a form submit using ajax instead of laravel to avoid page reload etc. But it isn't working and I don't know why. I've searched the wonderful web going through example after example but nothing seems to be working for me. This is the closest I can get. My knowledge is limited but my ambitions are high. Please take a moment and look through my code, beacuse I'm at the edge of mental breakdown right now.

这是我的jquery:

Here is my jquery:

$(document).ready(function() { $('#ajax').submit(function(event){ $.ajax({ type: 'POST', url: 'comments', data: $('form#ajax').serialize(), dataType: 'json', }) .done(function(data) { console.log(data); }); event.preventDefault(); }); });

这是我在刀片视图中的表单:

And here is my form in blade view:

{{ Form::open(array('url'=>'comments', 'id' => 'ajax')) }} {{ Form::hidden('parent_id', null) }} {{ Form::hidden('author', Auth::user()->username) }} {{ Form::textarea('content', null, array('placeholder' => 'Enter your comment here:', 'onfocus' => 'this.placeholder=""')) }}<br/> {{ Form::submit('Comment', array('class'=>'submit')) }} {{ Form::close() }}

这是我的控制器:

public function createComment() { //fixa här så man inte kan kommentera tom kommentar $validate = array('content' => 'required'); $validator = Validator::make(Input::all(), $validate); if($validator->fails()) { return Redirect::to('comments')->withErrors($validator)->withInput(Input::all()); } else { $comment = new Comment(); $comment->parent_id = Input::get('parent_id'); $comment->author = Input::get('author'); $comment->content = Input::get('content'); $comment->save(); } } public function postComment() { if(Request::ajax()) { $this->createComment(); return Response::json(Input::all()); } } public function getCommentsAndForm() { $comments = Comment::orderBy('id')->get(); return View::make('comment')->with('comments', $comments); }

这是我的路线:

Route::get('comments', array('uses' => 'CommentController@getCommentsAndForm')); Route::post('comments', array('uses' => 'CommentController@postComment'));

当我提交表格时,它会消失并且不显示任何内容。如果我删除 if(Request :: ajax()),评论会被发布,但表单仍然消失,而不是空页面我在JSON中获得发布的评论。当我重新加载页面时,评论显示为我想要的。 有什么问题?我究竟做错了什么?我只希望发布的评论显示在我的表单上方而不重新加载页面,是否有解决方案?

When I submit the form it dissapears and nothing displays. If I remove if(Request::ajax()) the comments get posted but the form still dissapears and instead of an empty page I get the posted comment in JSON. And when I reload the page the comments shows as I want to. What is the problem? What am I doing wrong? I only want the posted comment to display above my form without reloading the page, is there a solution?

推荐答案

$(document).ready(function() { $('#ajax').submit(function(event){ event.preventDefault(); $.ajax({ type: 'POST', url: 'comments', data: $('form#ajax').serialize(), dataType: 'json', }) .done(function(data) { console.log(data); }); //just to be sure its not submiting form return false; }); });

这里重要的是阻止表单提交。您可以尝试将提交更改为按钮。

the important here is to prevent form from submiting. You can try to change submit to button.

更多推荐

使用带有laravel的ajax提交表单时,避免页面重新加载

本文发布于:2023-11-24 22:32:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   加载   页面   laravel   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!