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

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 ?

图片转代码服务由CSDN问答提供 功能建议

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

 &lt; form class =“delete_confirm”action =“{{route('comment.delete_confirm',$ mypage-&gt; id)}}”&gt; 
&lt; input type =“hidden”name =“_ token”value  =“{{csrf_token()}}”&gt; 
&lt; input type =“password”name =“pass_con”placeholder =“비밀번호를입력하세요。”&gt; 
&lt; input type =“submit”class =  “pass_submit”value =“확인”&gt; 
&lt; / form&gt; 
   
 
 

JS

  $ del_conForm  .on('submit',function(){
 
 $ .ajax({
 url:'comment / {id}',
 type:'post',
 data:$(this).serialize  (),
成功:函数(res){
 if(!res){
 alert('not correct'); 
返回false; 
} 
 else {
 if(confirm(“Click” 可以继续吗?“)){
 $ delForm.submit(); 
} 
}  
} 
}); 
 
}); 
   
 
 

我想知道 $ mypage-&gt; id in HTML表单也是提交控制器。

控制器

  public function delete_confirm($ id){
 
 $ mypage = mypage:  :findOrFail($ id); 
 
 $ password = $ mypage-&gt; password; 
 
 $ data = Input :: get('pass_con'); 
 
 if($ data == $ password)  ){
返回'suc'; 
}其他{
返回false; 
} 
} 
   
 
 

路线 \ n

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

Request :: ajax()部分工作成功,但 $ password 什么都不返回。 控制器delete_confirm方法可以接收 $ id 吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题