0

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
  • Can you provide us with your route definition please? The parameter id needs to be declared there, too. Commented Nov 17, 2016 at 9:43
  • You forgot to put id in ajax url Commented Nov 17, 2016 at 9:43
  • @ReneM. I edited. I defined route like that. Commented Nov 17, 2016 at 9:44
  • Looks ok, I would use a step debuger to check if id is filled and if the query returns a result and is the result the result you expect. Commented Nov 17, 2016 at 10:10

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

11 Comments

try echoing $id in controller and you need to remove quotes around $id. $mypage = mypage::where('id', $id)->get();
Do one more thing check what url your ajax request hit using developer tool or simply put alert(url); inside submit event
removing quoutes around $id, $id is echoing successfully
Try to var_dump($my_page) and check what it return. May be password is empty or null
$mypage = mypage::findOrFail($id); var_dump($mypage); brings array normally.
|
0
$mypage = mypage::find($id);

$password = $mypage->password;

Comments

0

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.

Comments

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

        }
    });

});

1 Comment

why you write $($this).attr('action')? $this is already a jQuery object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.