By brenopsouza on
Hey ppl!
I'm stuck here with something I believe is quite simple, on my module, there's a form that, when submitted will add a row on my database, but the user who adds it wont be able to delete it, so when he clicks the 'submit' button I'd like to pop a confirm box so that only if he clicks yes the action is done... I told you, quite simple, isn't it??
But as I'm not a Javascript ace, I cant figure out how to do it, anyone with the knowhow????
Thx!
Comments
add this code to the the
add this code to the the submit button in you form
Let me know if it works for you
Arshad(wydle)
Huh?
...Forgive for I am so noob... hehe
But I'm implementing the hook form and so, its like this:
So... where shoud I put this code???
Thx!
Right
I did it! In case someone is having the same troubles as me, here's what i did:
To add the 'onSubmit', I used this line:
'#attributes' => array('onsubmit' => 'if(confirm('.$msg.')) return true; return false;'));That variable $msg over there I used to avoid issues with quotes...BUT I dont know why, the onsubmit didnt work (again, im not a javascript ace).
I achieved what I wanted by swapping the onsubmit by a onclick, and the code became this:
'#attributes' => array('onclick' => 'if(confirm('.$msg.')) return true; return false;'));And that's it!
Thx wydle!
Im glad you made it. and
Im glad you made it. and thanks for sharing the solution.This will be helpful for other community members. ;)
cleaner js code
instead of using
if (confirm(' . $msg . ')) return true; return false;would be cleaner to use
return confirm(' . $msg . ');Good Catch !
@brenopsouza you saved my life. Great solution!
Thanks a lot.
I would use the code below
I would use the code below for a few reasons:
1) It's properly integrated with the Drupal system
2) It maintains the separation of markup and function (html and javascript) by keeping the javascript external.
Note: The following variables should be adjusted to match your module:
1) moduleName should be changed to the name of your module
2) $msg should be changed to whatever message you want.
One other point to note is that it's actually better to not specifically name the #id in the submit button declaration, and to change #confirm_submit in the javascript to be the natural ID of the submit button rather than a forced one like I have done here.
Contact me to contract me for D7 -> D10/11 migrations.
+1
+1
Has anyone tried this method
Has anyone tried this method with AHAH callback? It could interfere with that can it not?