0

I ask the user "Are you sure?" when clicking on a form submit button:

{{ Form::open(array('url' => 'myhome/pic-remove', 'onsubmit' => 'return confirm(\'Are you sure?\');')) }}

And I deactivate the submit button on click:

$('#submitbutton').click(function() {
    $('#submitbutton').hide();
    $('#isloading').show();  
});

The Problem is, the submit button is also deactivated, if the user 'is not sure' and clicks 'cancel'.

How can I deactivate the submit button only if user 'is sure' on the first question and clicks 'ok' ?

edit:

ok i have it like this now, seems to work

$('#submitbuttonconfirm').click(function() {
    var r = confirm('Are you sure?');
    if (r == true)
    {
        $('#submitbuttonconfirm').hide();
        $('#isloading').show();
        return true;
    }
    else
    {
        return false;
    }      
});
haheute
  • 2,129
  • 3
  • 32
  • 49

2 Answers2

2

I think the problem is, that the click event is fired before the submit event, so you hide the submit button before you even ask the user.

You can use jQuery to handle submit event instead of of using click and disable the button there:

{{ Form::open(array('url' => 'myhome/pic-remove')) }}
$('form').submit(function(e) { // Use the id of the form here, if you have more than one
    if(confirm('Are you sure?')) {
        $('#submitbutton').hide();
        $('#isloading').show();
    } else {
        return false;
    }
});
Jan
  • 1,394
  • 10
  • 12
  • is this e necessary? i think it stands for 'event', but what is it doing? – haheute Nov 25 '13 at 19:22
  • 1
    No, sorry. It is not necessary. You are right, it is the event object. Generally you should use e.preventDefault() to prevent the default event (submit the form) from executing. However I think some old browsers may not support it. See here for a better explanation: http://stackoverflow.com/a/1357151/1798215 – Jan Nov 25 '13 at 19:26
0

I assume the top section is you sending the JavaScript to be executed from the server to the client. I'm not sure why you're doing that instead of writing it as JavaScript directly but I suppose it doesn't matter. As Givi said, get the response from the confirm dialog and then use it as a decision point. Since I'm not sure what this is, I'm only guessing as to this code but it should give you an idea.

{
    { Form::open(
        array(
            'url' => 'myhome/pic-remove',
            'onsubmit' => 'if(confirm(\'Are you sure?\')){ $(\'#submitbutton\').hide(); $(\'#isloading\').show(); return true; } return false; '
        )
    ) }
}
Uptown Apps
  • 707
  • 1
  • 5
  • 11