سؤال

I'm not sure if my error comes from submitting the form to itself via PHP but it is just not hitting my function and submits anyway no matter what is returned. I'm not sure what I'm missing here. This is my form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Login Page</title>
    <script type="text/javascript" src="js/functions.js"></script>
    <!--Internal for simplicity or until bigger page -->
    <style type="text/css"> 
        #login div{display:inline;color:red;}
        input{margin-right:20px;}
    </style>
</head>
<body>
<form name="login" id="login" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" onsubmit="return CheckLogin()">
    <?php if($_POST && $win == false): ?>
        <div>Login Unsuccessful. Please Try Again</div><br />
    <?php endif ?>
        <input type="text" size="10" name="username" /><div></div>
            <br />
        <input type="password" size="10" name="password" /><div></div>
            <br />
    <input type="submit" text="Submit" />
</form>
<a href="register.php">Registration Page</a>
</body>
</html>

Really I'm just checking if username/password are empty but it just submits anyway going right through this function:

function CheckLogin()
{
    window.alert("made it here");
    var login = document.forms["login"];
    var user = login["username"].value;
    var pass = login["password"].value;

    if(user == null || user == "")
    {
        window.alert("user");
        return false;
    }  

    return false;
}

I'm sure it's something stupid that I'm missing so before I submitted this I double checked the function names and to see if I was getting any javascript errors but neither turned out to be the problem. Does anybody see my error?

The point being I do not want the PHP to run or the form to be submitted to itself if username or password is empty or null.

EDIT Added full html page minus the PHP functionality. Also noted that for some reason my javascript link does not 'link' instead I can view the contents of it directly in the header like it is embedded via inspect element or firebug. This might be the root of the problem but not sure why.

EDIT Added doctype just incase, but still no help. It is automatically embedding my javascript into my page and putting it into CDATA like you would normally instead of actually linking it. It does this in IE, Firefox & Chrome...

if anyone thinks my PHP might have something to do with it I could post it, but I don't see how server side code could interact with client side code without being posted.

هل كانت مفيدة؟

المحلول

There are some minor errors in your code. First of all there is no attribute called "text" for an input, I believe that you missed it :) Try to validate them first. Check here to see the attributes that an input may take. Second of all, this jsfiddle example works properly in Chrome and FF. I cannot test in IE since I am using Ubuntu.

After I corrected the form attributes, the example in jsfiddle started to work. I am not sure whethere it's related with that, but apparently that seems the only logical explanation to me for your code not working.

نصائح أخرى

Your code runs fine. Try running it in another web browser. It's possible that one of the extensions you installed on your browser is causing a JS error (having Google Dictionary on Chrome was causing issues for me).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top