当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Form auto-submitting on page load

问题描述:

How can I stop my form from auto-submitting? Currently the controller function (deleteAccount) indicated below is executed immediately the page is loaded whereas it should only be triggered upon clicking the button.

The relevant routes:

Route::resource("myaccount", "MyAccountController");

Route::get("myaccount", "[email protected]");

The relevant Controller function:

public function deleteAccount(){

$user = \Auth::user();

\Auth::logout();

$user->delete();

flash()->success("Account Deleted Successfully!");

return redirect("/");

}

The relevant view code:

{{ Form::open(array('action' => '[email protected]')) }}

{{ method_field('get') }}

<button type="submit">Delete Account!</button>

{{ Form::close() }}

网友答案:

Try to write your function as follows

public function deleteAccount(){    
if(isset($_GET)){
    $user = \Auth::user();

    \Auth::logout();

    $user->delete();

    flash()->success("Account Deleted Successfully!");

    return redirect("/");
}} 
网友答案:

Thanks for your response @Mitesh Pant. Turns out this code was just fine. The only blunder I made was "wrap" this form within another form in a vain attempt to style my form buttons. I should have just used the good old div tag! I think as a result DOM couldn't execute the hidden delete method even though it could read it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: