简体   繁体   中英

JavaScript Form Validation Still Submits Form on 'False'

Question as stated above. I am using older browsers (IE8 and FF8) due to corporate policy. I can't fix this but my research says this isn't the issue...(yeah right;)

I'm using PHP 5.5.12 on Apache 2.4.9 with a MySQL (5.6.17) back end. The site is a CMS that has grown organically over several years.

I have a user admin page that adds, updates, and deletes accounts. However, no matter what I have done the form submits. The worst example is if the admin chooses to delete an account and when asked 'Do you really wish to DELETE...?' and cancels, it is still deleted! I've copied my JavaScript and an excerpt from the PHP/HTML below.

I have tried a few changes to my JavaScript like returning after setting the window.event.returnValue but I've always gotten the same results. I've been reading for several days now and keep coming up blank! I've tried onSubmit instead of onClick but it really doesn't suit the site, besides it didn't work either.

I'm beginning to think the age of the browsers is the issue. I run this with Safari on my home development box fine. Any help would be appreciated.

JavaScript

<script language="JavaScript">
function frmVerify(check, loginid, login) {
   if (check == 'add') {
      Uname=add_user.login_name.value;
      Pass1=add_user.password.value;
      Pass2=add_user.password2.value;
      if(Uname=='') {
          alert('A user name must be assigned to an account.');
          window.event.returnValue=false;
      }

      if(Uname == login) {
          alert('You cannot create an account with your own username (' + login + ')');
          window.event.returnValue=false;
      }

      if(Pass1 != Pass2) {
          alert('Entered passwords are not the same!  Make sure the password and verification fields match.');
          window.event.returnValue=false;
      }

      if(Pass == '') {
          alert('Assigning a password is required when creating an account!');
          window.even.returnValue=false;
      }
   } else if(check == 'update') {
      Uname=eval('edU_'+loginid+'.login_name.value');
      Pass1=eval('edU_'+loginid+'.password.value');
      Pass2=eval('edU_'+loginid+'.password2.value');
      if(Uname == '') {
          alert('A user name must be assigned to an account.');
          window.event.returnValue=false;
      }

      if(Pass1 != Pass2) {
          alert('Entered passwords do not match!  Make sure the passowrd and verification fields match.');
          window.event.returnValue=false;
      }
   } else if(check == 'del') {
      Uname=eval('edU_'+loginid+'.login_name.value');
      if(Uname == '') {
          request = 'Do you really wish to DELETE this user account?';
      } else {
          request = 'Do you really wish to DELETE user: ' + Uname + '?';
      }

      var answer = confirm(request);
      if(answer) {
         window.event.returnValue=true;
      } else {
         window.event.returnValu=false;
      }
   }
}
</script>

In the PHP/HTML I have

echo "<form name=\"delU_$login_id\" id=\"delU_$login_id\" mehtod=\"POST\">";
echo "<input type=\"image\" src=\"./images/delete.png\" value=\"Delete\" onClick=\"return frmVerify('del', '$login_id', '$username');\">";
echo "</form>";

window.event.returnValue (which you don't always spell correctly anyway) is non-standard and shouldn't be used.

You're using 1990s style intrinsic event attributes instead of addEventListener , so just:

return false;

If you were using addEventListener then you would:

event_object.preventDefault();

where event_object is the first argument to your event handler function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM