laravel如何使用ajax获取数据库值?

时间:2016-11-17 09:37:13

标签: javascript php jquery ajax laravel

我想使用ajax将此表单发布到Controller。

<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){
                if (!res) {
                    alert('not correct');
                    return false;
                }
                else{
                    if (confirm("Click OK to continue?")) {
                        $delForm.submit();
                    }
                }
            }
        });

    });

我想知道HTML表单中的$mypage->id也是提交控制器。

控制器

public function delete_confirm($id) {

    $mypage = mypage::findOrFail($id);

    $password = $mypage->password;

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

    if ($data == $password) {
        return 'suc';
    }else{
        return false;
    }
}

路线

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()部分工作成功,但$password没有返回任何内容。控制器delete_confirm方法可以接收$id吗?

4 个答案:

答案 0 :(得分:1)

试试这个:

$del_conForm.on('submit', function(){
    url = $(this).attr("action");
    $.ajax({
        url: url,
        type: 'post',
        dataType : 'json'
        data: $(this).serialize(),
        success: function(res){
            if (!res.status) {
                alert('not correct');
                return false;
            }
            else{
                if (confirm("Click OK to continue?")) {
                    $delForm.submit();
                }
            }
        }
    });

});

//您的控制器代码应如下所示

public function delete_confirm($id) {

    $mypage = mypage::findOrFail($id);

    $password = $mypage->password;

    $data = Input::get('pass_con');
    $response = array();
    if ($data == $password) {
       $response['status'] = true;
    }else{
       $response['status'] = false;
    }
    return response()->json($response);
}

答案 1 :(得分:0)

$mypage = mypage::find($id);

$password = $mypage->password;

答案 2 :(得分:0)

你的代码中有一个错误,你把$ id放在引号之间,这样它就像一个字符串而不是变量。 此外,您可以更改使用请求的方式

(假设你正在使用Laravel 5)

public function delete_confirm(Request $ request,$ id){

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

$password = $mypage->password;

if($request->ajax()) {

    $data = $request->get('pass_con');

    return $password;
}

}

此外,我不知道您真正想做什么,但您可以使用Auth::attempt来验证密码(请参阅https://laravel.com/docs/5.3/authentication

您还可以在代码中放置一些var_dump($id)来检查变量$ id是否真的已设置。

答案 3 :(得分:0)

// use dedication methods to make it workable most of the time
$(document).on('submit','.delete_confirm', function(e){
    e.preventDefault();
    $this = $(this);
    $.ajax({
        url: $($this).attr('action'),
        // use method for get,post and others and keep in mind all method values should be in block letters
        method: 'POST',
        // error is right here you just use this keyword but under ajax this refers to the ajax object of jquery so you have to create $this object like above
        data: $($this).serialize(),
        success: function(res){
            console.log(res);

        }
    });

});