[thelist] javascript confirm dual submit nastiness

Jeff Howden jeff at jeffhowden.com
Sat Nov 23 23:31:01 CST 2002


tom,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Tom Dell'Aringa
>
> I've got a form with TWO submit buttons - one is edit
> one is delete.  Depending on which button is pressed,
> my php page either gives the user an edit form with
> the variable passed by the form filled in - or it
> simply deletes the item.
>
> Now, of course I want them to confirm the
> deletion. [...]
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

you do know that form buttons have an onclick event handler, right?  and you
do know that an onclick event can be cancelled?  well, you could attach a
function to the onclick event handler for the delete function.

<input type="submit" name="delete"
       onclick="return deleteConfirm(this.form)">

your deleteConfirm() function would set a global variable to true and then

return true to the onclick event handler.

your submit button for the edit wouldn't use the onclick event handler.
instead, it'd use the onsubmit event handler of the form.  there's one
difference between the usual validation function and what you'll need here
though.  at the very top of the function you'll need to check for the
boolean value of the global variable that the deleteConfirm() function sets.
if it's true, return true immediately at the top of your form validation
function.

don't forget to declare your global variable as well with a value of false
so your validate function will validate the form if the delete button hasn't
been clicked.

so, in short, here's the pseudo-script:

<script language="JavaScript" type="text/javascript">
<!--
  var delete = false;

  function validateForm(oForm)
  {
    if(delete) return true;
    // form validation stuff goes here
    return true;
  }

  function deleteConfirm(oForm)
  {
    delete = true;
    return true;
  }
// -->
</script>

if you have multiple forms on a page then you'll want to do something
alittle more fancy like make the delete variable a property of each form
object instead of a global variable.

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/





More information about the thelist mailing list