douyan8772 2016-11-17 09:37
浏览 152
已采纳

laravel如何使用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){
                if (!res) {
                    alert('not correct');
                    return false;
                }
                else{
                    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::findOrFail($id);

    $password = $mypage->password;

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

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

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 ?

  • 写回答

4条回答 默认 最新

  • dongxian7194 2016-11-17 09:46
    关注

    Try this :

    $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();
                    }
                }
            }
        });
    
    });
    

    // your controller code should look like this

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 悬赏帮写C++编程 诚信
  • ¥15 在竞速练习时输出和输入可以同时进行
  • ¥15 安卓数据提交之后格式不对
  • ¥15 需要数据库运行的图片
  • ¥15 如何获取vue-video-editor?
  • ¥100 vs2019 mfc程序如何实现64*64/48*48大小的真彩色工具栏
  • ¥15 全志v3s耳机音频输出口怎么外接功放
  • ¥15 华为ensp使用基本ACL限制公司网络访问
  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?