김정명 김정명 - 19 days ago 5
Javascript Question

laravel How to get database values using ajax?

I want to post this form to Controller using ajax.

<form class="delete_confirm" action="{{ route('comment.delete_confirm', $mypage->id) }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="password" name="pass_con" placeholder="비밀번호를 입력하세요.">
<input type="submit" class="pass_submit" value="확인">
</form>


JS

$del_conForm.on('submit', function(){

$.ajax({
url: 'comment/{id}',
type: 'post',
data: $(this).serialize(),
success: function(res){
alert(res);
/*
if (confirm("Click OK to continue?")){
$delForm.submit();
}
*/
}
});

});


I want to know
$mypage->id
in HTML form is also submit controller.

Controller

public function delete_confirm($id) {

$mypage = mypage::where('id', '$id')->get();

$password = $mypage->password;

if(Request::ajax()) {

$data = Input::get('pass_con');

return $password;
}
}


Route

Route::get('comment/{id}', ['as'=>'comment.delete_confirm', 'uses'=>'MyPageController@delete_confirm']);
Route::post('comment/{id}', ['as'=>'comment.delete_confirm', 'uses'=>'MyPageController@delete_confirm']);


Request::ajax()
part is work successfully, but
$password
returns nothing. Can controller delete_confirm method receive
$id
?

Answer

Try this :

$del_conForm.on('submit', function(){
    url = $(this).attr("action");
    $.ajax({
        url: url,
        type: 'post',
        data: $(this).serialize(),
        success: function(res){
            alert(res);
            /*
            if (confirm("Click OK to continue?")){
                $delForm.submit();
            }
            */
        }
    });

});
Comments