Start by using console.log(response) to see what data you get returned
Sep 10, 2022
12
Level 7
Get data after submit form..
Hi everyone!! I have used axios library to submit form but , the results don't appear on page but form submitted successfully but if i refresh the page the data appear. my question : how can i get data and show it from response of form??
<section class="mb-10 col-span-8 col-start-5 mt-10 space-y-6">
@include('books.__add-comment-form')
@foreach($book->comments as $comment)
{{-- the data i want to see here.. --}}
<x-book-comments :comment="$comment"/>
@endforeach
</section>
@auth
<x-panel>
<form
action="/books/{{$book->title}}/comments" method="POST"
>
@csrf
<header class="flex items-center">
<img
src="https://i.pravatar.cc/50?u={{auth()->id()}}"
width="50" height="50" alt=""
class="rounded-full "
>
<h2 class="ml-4 font-semibold">Want to participate?</h2>
</header>
<div class="mt-4">
<textarea
class="w-full p-2 text-sm focus:outline-none focus:ring"
name="body"
id="body"
rows="5"
placeholder="Quick,think of something to say!"
></textarea>
<x-form.errors name="body"/>
</div>
<div class="flex justify-end mt-6 pt-6 border-t border-gray-200 ">
<x-form.button id="submit-button">Publish</x-form.button>
</div>
</form>
</x-panel>
@else
<p class="text-blue-500 font-bold">
<a class="hover:underline"
href="/register">Register</a> or
<a class="hover:underline"
href="/login">Log in</a> to
leave a comment.
</p>
@endauth
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js"></script>
<script>
var comments = document.getElementById('comments');
document.getElementById('submit-button').addEventListener("click", function(event){
event.preventDefault()
var body = document.getElementById('body').value
axios.post('/books/{{$book->title}}/comments', {body: body})
.then(function (response) {
// here how can i get data and show it after submit the form???
})
.catch(function (error) {
console.log(error);
});
})
</script>
</x-app-layout>
Please or to participate in this conversation.