Reputation: 319
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
?
Upvotes: 0
Views: 1381
Reputation: 2708
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);
}
Upvotes: 1
Reputation: 3579
// 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);
}
});
});
Upvotes: 0
Reputation: 182
There is one error in your code, you put $id between quote so it's like a string and not the variable. Also, you could change the way you use the request
(This is assuming you're using 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;
}
}
Also, I don't know what you really want to do, but you can use Auth::attempt
in order to valid a password (see https://laravel.com/docs/5.3/authentication )
You can also put some var_dump($id)
in your code to check if the variable $id is really setted.
Upvotes: 0
Reputation: 28529
$mypage = mypage::find($id);
$password = $mypage->password;
Upvotes: 0