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

shadcn’s picture

add this code to the the submit button in you form

<input type="submit" onSubmit="if(confirm('Message here')) return true; return false;">

Let me know if it works for you
Arshad(wydle)

brenopsouza’s picture

...Forgive for I am so noob... hehe
But I'm implementing the hook form and so, its like this:

function tutor_form($form_state) {
	$form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#size' => 40,
    '#required' => TRUE,
    '#description' => t('Put the description'),
  );
  	$form['valor'] = array(
    '#type' => 'textfield',
    '#title' => t('Value'),
    '#size' => 7,
    '#required' => TRUE,
    '#description' => t('Put the value.'),
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}

So... where shoud I put this code???

Thx!

brenopsouza’s picture

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!

shadcn’s picture

Im glad you made it. and thanks for sharing the solution.This will be helpful for other community members. ;)

arhak’s picture

instead of using
if (confirm(' . $msg . ')) return true; return false;
would be cleaner to use
return confirm(' . $msg . ');

operations’s picture

@brenopsouza you saved my life. Great solution!

Thanks a lot.

jaypan’s picture

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.

function hook_form($node, $form_state)
{
  $msg = t('Do you want to submit this?');
  $jscript = 'Drupal.behaviors.moduleName = function() {$("#confirm_submit").click(function(){return confirm("' . $msg . '")});}';
  drupal_add_js($jscript, 'inline');
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#id' => 'confirm_submit',
    '#value' => t('Submit'),
  );
  return $form;
}

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.

arhak’s picture

+1

executex’s picture

Has anyone tried this method with AHAH callback? It could interfere with that can it not?