Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

submitOK - not working properly? 1

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
CA
Hi all,
I have a form which enables the user to delete a field. Using javascript, I prompt the user with the: "Are you sure you want to do this? message.

javascript code:
Code:
  submitOK = confirm( '\n Delete \"' + my_name + '\" ?\n\r It is highly recommended you do NOT delete any Cities.');
  if (!submitOK)
    document.location.reload();
if (validHandle == false && submitOK) {
    errorString += "\r";
    alert(errorString+'\nPlease make changes and submit the form again.');
  }
  if (validHandle == true) {
    return (validHandle == true && submitOK);
  }

html form is calling the javascript function properly.

I get the alert:
Delete (cityname)?

It is highly recommended you do NOT delete any Cities.

When I click on cancel, the form still submits.

Any ideas?

[cheers]
Cheers!
Laura
 
Code:
submitOK = confirm( '\n Delete \"' + my_name + '\" ?\n\r It is highly recommended you do NOT delete any Cities.');

if (!submitOK) {
   if (validHandle == false) {
       errorString += "\r";
       alert(errorString+'\nPlease make changes and submit the form again.');
       return false;
   }
   if (validHandle == true) {
      return true;
   }
}

document.location.reload();
return false;

Above I have re-arranged code to skip the validation checks when the person clicks Cancel. Then reload the page.

Place this code in a function and call the function with the onsubmit="return isValid()" event handler in the form tag.

If any bad things happen in the validation checks exit the function with return false;. At the very end of the validation function which will only be reached if all validation checks were successful return true.

If the function returns false the form will not submit.
Also return false if they click Cancel.

Using document.location.reload() will recreate a blank form. Is that what you wish?

Actually, I am unsure that the above code will ever get to the last line and return false. Cause if the page reloads, what happens to the Javascript? So it may submit because we haven not returned a false value.

 
Though a re-ordering per rac2 is a good idea, I see some problem there. I would prefer this, if I understand the op's latent logic.
[tt]
submitOK = confirm( '\n Delete \"' + my_name + '\" ?\n\r It is highly recommended you do NOT delete any Cities.');

if (!submitOK) {
document.location.reload();
return false;
}
//here after only the user shown a will to submit
if (!validHandle) {
errorString += "\r";
alert(errorString+'\nPlease make changes and submit the form again.');
}
//the return will eventually determined by validHandle
return validHandle;
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top