Answer the question
In order to leave comments, you need to log in
Why are nulls coming from the ajax comment form?
comment form
<div class="col-md-12" style="margin-top:20px;">
<div class="card">
<div class="card-header"><h3>Оставить мнение</h3></div>
<form id="commentCreate">
@csrf
<input type="hidden" name="post_id" id="post_id" value="{{ $article->id }}">
<input type="hidden" name="comment_id" value="0">
<div class="card-body">
<div class="input-group" >
<input class="form-control" type="text" name="name" id="name" placeholder="Ваше имя">
<input class="form-control" type="text" name="email" id="email" placeholder="Ваш Email">
</div>
<div class="input-group" style="margin-top:20px;">
<textarea class="form-control" name="comment" id="comment" rows="3"></textarea>
</div>
<div class="col-auto" style="margin-top:20px;">
<button type="submit" class="btn btn-success mb-3">Коментировать</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="{{ asset('public/js/jquery-3.5.1.min.js') }}"></script>
<script type="text/javascript">
$( document ).ready(function() {
$('button').click( function(event) { event.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
var comment = $('#comment').val();
var post_id = $('#post_id').val();
$.ajax({
url: 'api/store',
type: "POST",
data: {name:name, email:email, comment:comment}, post_id:post_id,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
dataType: 'json',
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data);
//$(this).parent().append(
'<div class="col-md-12" style="margin-top:20px;" ><div class="card"><div class="card-header">+name+</div>
<div class="card-body">+comment+</div></div></div>
');
}
});
});
});
</script>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\user;
use App\Blog_article;
class CommentController extends Controller
{
public function store(Request $request)
{
dd($request->all());
$comment = Comment::add($request->all());
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question