2016-11-17 5 views
0

このフォームをajaxを使用してControllerに投稿したいと思います。laravel 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(); 
        } 
       } 
      } 
     }); 

    }); 

私は、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'=>'[email protected]_confirm']); 
Route::post('comment/{id}', ['as'=>'comment.delete_confirm', 'uses'=>'[email protected]_confirm']); 

Request::ajax()一部が正常に仕事ですが、何も$password返しません。コントローラdelete_confirmメソッドは$idを受信できますか?

+0

のようになります。あなたはあなたのルート定義を私たちにしてください提供することはできますか?パラメータIDもそこで宣言する必要があります。 –

+0

あなたはajax urlにidを入れるのを忘れました –

+0

@ReneM。私は編集しました。私はそのようなルートを定義しました。 – jungmyung

答えて

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); 
} 
+0

コメントありがとうございます。しかし、ページは何も警告しません:(... – jungmyung

+0

コントローラで$ idをエコーし​​て、$ idの周りの引用符を削除する必要があります。 $ mypage = mypage :: where( 'id'、$ id) - > get() ; –

+0

あなたのajaxリクエストが開発ツールを使用してヒットしたかどうかを確認するか、単にアラート(url)を入力してください( –

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

$password = $mypage->password; 
+0

何も変更されていません.. – jungmyung

+0

var_dump your $ mypage。それが表示されるwharエラー –

0

コードに1つの誤りがあります。引用符の間に$ idを入れて、変数ではなく文字列のようにします。 また、あなたは(これは、あなたがLaravel 5を使用していると仮定している)

パブリック関数delete_confirm(リクエスト$要求は、$ 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が実際に設定されているかどうかを確認することもできます。

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); 

     } 
    }); 

}); 
+0

これはあなたのために働くと思う...私に知らせてください... –

+0

$($ this).attr( 'action')をなぜ書くのですか? $ thisはすでにjQueryオブジェクトです –

+0

あなたは両方の方法で書くことができます... $ thisまたは$($ this).... –

関連する問題