Some Javascript Help

  • Thread starter Spock
  • 2 comments
  • 471 views

Spock

Not In My Name
Premium
3,341
So here's the deal, currently I am writing a php/sqlite CMS system and I am using some JavaScript to do some form checking before sending the data off to the server for processing. The problem I am running into is my JS code has just stopped working for the most part

this is what the script boils down to

Javascript
Code:
function check_install()
{
    valid = true;
    
    if( document.Installer.AdminName.value == "" )
    {
        alert("Administrator Name Is Empty");
        valid = false;
    }
   
    return valid;

}
Form code
Code:
<form name="Installer" action="installer.php?action=runInstall" method="post" onsubmit="return check_install();">
   <input type="text" name="AdminName" />
   <input type="submit" name="submit" value="Install" />
</form>
So if any tag within the Installer form trips an empty value, the form doesn't submit. The problem I am having is that exact code was working for me two days ago, the only thing I have changed is adding some more IF checks for some other form values, and I know they are being checked as leaving them empty throws the alert box, but even though the form is getting a false returned to it, it still gets submitted.

This is frustrating because I can do full OOP programming in php, the whole backend of my CMS is module based and miles more complex, but this little js code doesn't work. What gets me even more I am trying to keep all the code in this as 100% my own, so I really don't want to go out and grab some bloated form validators, when this code is more than enough for my needs.

So are there any JS gurus that can help me?
 
I haven't done JS in a while but should "Valid = False;" read "Return False;", then replace the "Valid = True;" with a "Return True;" down the bottom?
 
Well if one of the IF checks gets an empty value, it trips the valid to be false. So say the admins name is empty, the form becomes invalid, an alert is set saying the admin name is empty, and FALSE is returned, thus the browser shouldn't send the data.
If the admins name is filled out, the IF statement doesn't check it since its not empty, thus true is returned since there wasn't an empty field to indicate an invalid form, and the data is passed back to the script and the install continues.

The problem is that that exact code has stopped working and no matter if the name is empty or filled out, the form is still submitted. I still get a popup saying the admin name is empty, but when I close the alert, the data is still sent, and installed with empty names/passwords/and so on.

Edit: I'm still boggled by this, I guess I'll just move the checks serverside.
 
Last edited:
Back