javascript - getting already posted post fields and updating them using jquery -
i'm working on laravel, want edit post having multiple fields edit. i've done post's body field , want achieve other two.
related controller
code:
$post->body=$data['body']; $post->state = $data['state']; $post->problem_domain = $data['problem_domain']; $post->update(); return response()->json(['new_body'=>$post->body],500);
blade
earlier data needs fetched:
<article class="post" data-postid="{{ $post->id }}"> <p> {{ $post->body }} </p> <p> <b> region: </b> {{ $post->state }}   <b> problem domain: </b> {{ $post->problem_domain}}</p> <div class="info"> posted <b>{{ $post->user->name }}</b> on <b>{{ $post->created_at }}</b> </div> <div class="interaction"> <a href="#" class="like">{{ auth::user()->likes()->where('post_id',$post->id)->first() ? auth::user()->likes()->where('post_id',$post->id)->first()->like == 1 ? 'you post':'like':'like'}} </a> <a href="#" class="like">{{ auth::user()->likes()->where('post_id',$post->id)->first() ? auth::user()->likes()->where('post_id',$post->id)->first()->like == 0 ? 'you don\'t post':'dislike':'dislike'}}</a> @if(auth::user() == $post->user) <a href="#" class="edit" >edit</a>
and js file
,please @ format body
correct , want same other two
var postid=0; var postbodyelement=null; $('.post').find('.interaction').find('.edit').on('click',function(event){ event.preventdefault(); postbodyelement=event.target.parentnode.parentnode.childnodes[1]; var postbody=postbodyelement.textcontent; postid=event.target.parentnode.parentnode.dataset['postid']; $('#post-body').val(postbody); $('#edit-modal').modal(); }); $('#modal-save').on('click',function() { $.ajax( { method : 'post', url : urledit, data: {body: $('#post-body').val(), state: $('#post-state').val(), problem_domain: $('#post-problem_domain').val(), postid: postid, _token: token} }) .done(function(msg) { $(postbodyelement).text(msg['new_body']); $('#edit-modal').modal('hide'); }); } );
at controller, you're returning body @ line:
return response()->json(['new_body'=>$post->body],500);
you should either return complete model or other attributes, this:
return response()->json($post, 500);
and in blade update other attributes.
Comments
Post a Comment