使用AJAX提交表单laravel [英] Submit form laravel using AJAX
问题描述
我正在尝试使用AJAX技术添加评论,但我有一个错误:
无法加载资源:http:// localhost:8888 / blog / public / comment / add the server回复状态为500(内部服务器错误)
这是我的代码:
查看:
I am trying to add comment using AJAX technology but I have an error:
Failed to load resource: http://localhost:8888/blog/public/comment/add the server responded with a status of 500 (Internal Server Error)
Here is my code:
View:
{{ Form::open(array('method'=>'post','class'=> 'col-md-6','url' => '/comment/add', 'id'=>'comment')) }}
<input type="hidden" name="post_id" value="{{$id}}">
<div class="row">
<div class="inner col-xs-12 col-sm-12 col-md-11 form-group">
{{Form::label('name', 'Imię')}}
{{Form::text('username', null, array('class'=>'form-control', 'id'=>'name', 'name'=>'name'))}}
</div>
<div class="inner col-xs-12 col-sm-12 col-md-12 form-group">
{{Form::label('message', 'Wiadomość')}}
{{Form::textarea('message', null, array('class'=>'form-control', 'id'=>'message', 'name'=>'message', 'rows'=>'5'))}}
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-12 submit form-group">
{{Form::submit('Wyślij', array('name'=>'submit', 'class'=>'btn btn-orange'))}}
</div>
</div>
{{ Form::close() }}
控制器:
public function addComment()
{
$this->layout = null;
//check if its our form
if(Request::ajax()){
$name = Input::get( 'name' );
$content = Input::get( 'message' );
$comment = new Comment();
$comment->author = $name;
$comment->comment_content = $content;
$comment->save();
$postComment = new CommentPost();
$postComment->post_id = Input::get('post_id');
$postComment->comment_id = Comment::max('id');
$postComment->save();
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return 'yea';
}else{
return 'no';
}
}
AJAX:
jQuery( document ).ready( function( $ ) {
$( '#comment' ).on( 'submit', function(e) {
e.preventDefault();
var name = $(this).find('input[name=name]').val();
$.ajax({
type: "POST",
url: host+'/comment/add',
}).done(function( msg ) {
alert( msg );
});
});
});
最后一条路线:
Route::post('comment/add', 'CommentController@addComment');
任何人都知道问题出在哪里以及为什么我不能提交表格?
Anyone have an idea where is the problem and why I can't submit my form?
推荐答案
您没有发布任何数据,
$.ajax({
type: "POST",
url: host+'/comment/add',
}).done(function( msg ) {
alert( msg );
});
您得到的错误是DB中的列不能为空。
The error you are getting is that the columns in DB cannot be null.
尝试将您的ajax调用更改为:
Try to change your ajax call to this:
$.ajax({
type: "POST",
url: host+'/comment/add',
data: { name:name, message:message, post_id:postid },
success: function( msg ) {
alert( msg );
}
});
更改此
var name = $(this).find('input[name=name]').val();
到
var name = $('#name').val();
并获取邮件和帖子ID:
and fetch the message and the post id:
var message = $('#message').val();
var postid = $('#post_id').val();
完成ajax块:
$('#comment').on('submit', function(e) {
e.preventDefault();
var name = $('#name').val();
var message = $('#message').val();
var postid = $('#post_id').val();
$.ajax({
type: "POST",
url: host+'/comment/add',
data: {name:name, message:message, post_id:postid}
success: function( msg ) {
alert( msg );
}
});
});
最后,在隐藏字段中添加ID:
And finally, add an ID to the hidden field:
<input type="hidden" name="post_id" id="post_id" value="{{$id}}">
从Laravel控制器发回数据,例如
Send data back from Laravel controller, eg.
// ........
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return Response::json($response); // <<<<<<<<< see this line
}else{
return 'no';
}
}
这会将您回复的数据发回给您ajax请求。
This will send the data in your response back to your ajax request.
然后,改变你的ajax成功函数:
Then, alter your ajax success function:
// .......
success: function( msg ) {
$("body").append("<div>"+msg+"</div>");
}
// ..........
现在,您将看到在< body>
中创建了一个新div,包括创建的响应。如果您想要显示新创建的帖子,只需将其创建为ajax响应并将其附加到页面中的任何元素。
You will now see that a new div was created in your <body>
including the created response. If you want to show the newly created post, just create it as the ajax response and append it to any element in your page.
这篇关于使用AJAX提交表单laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!