form not submitted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mishrask
    New Member
    • May 2012
    • 2

    form not submitted

    Code:
    function validlogin() {
    	userin = document.getElementById("user"); passin = document.getElementById("pass");
    	msg = document.getElementById("error");
    	user = userin.value; pass = passin.value;
    	valid = false; error = ""; k = 0;
    	if (user=="" || user==userin.defaultValue) { error = "<p>Your Username please...</p>"; k += 1; }
    	if (pass=="" || pass==passin.defaultValue) { error = "<p>Your Password please...</p>"; k += 1; }
    	if (k==2) { error = "<p>Your Username and Password please...</p>"; }
    	if (k!=0) { showDialog('',error); }
    	else {
    		obj = ajax();
    		obj.onreadystatechange = function() {
    			if(obj.readyState==4) {
    				error = obj.responseText;
    				if(error=="") { valid = true; }
    				else { showDialog('',error); }
    			}
    		}
    		obj.open("GET","loginchk.php?name="+user+"&pass="+pass,true);
    		obj.send(null);
    	}
    	return valid;
    }
    Above function is checking for username & password from a "loginchk.p hp" page. However, if username & password matches with those in the database, then variable "valid" is returned as true. All this is working fine.

    However, even if "valid" is true, html page is not submitting the form. Code for html is:

    Code:
    <?php
    if ( isset($_POST['btn_login']) ) {
    	$username = $_POST['user'];
     	$check = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    	while ( $info = mysql_fetch_array($check) ) {
    		$username = stripslashes($username);
    		$pass = md5(stripslashes($_POST['pass']));
    		$hour = time() + 7200;
    		setcookie(user_id, $info['id'], $hour);
    		setcookie(user_name, $username, $hour);
    		setcookie(user_key, $pass, $hour);
    		header('Location: ./members/');
    	} 
    } 
    else {
    ?>
    	<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" onsubmit="return validlogin()"><div id="login">
    		<input type="text" id="user" maxlength="30" class="text" value="Username" /><br />
    		<input type="password" id="pass" maxlength="30" class="text" value="Password" /><br />
    		<input type="submit" id="btn_login" class="default" value="Login" onmouseover="hover(this)" onmouseout="out(this)" onmousedown="klick(this)" onmouseup="hover(this)" />
    		<div id="error" class="hid"></div>
    	</div></form>
    <?php
    }
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s because of the asynchronous nature of your function. the script doesn’t stop after the send() call (because it’s asynchronous) and therefore valid is still false when you return.

    Comment

    • mishrask
      New Member
      • May 2012
      • 2

      #3
      Originally posted by Dormilich
      that’s because of the asynchronous nature of your function. the script doesn’t stop after the send() call (because it’s asynchronous) and therefore valid is still false when you return.
      Thanks for the prompt reply.
      When I see the value of the variable valid (in firebug console), it is shown as true. Doesn't that mean valid is returned as true by the script? Also, is there any alternative?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        it shows as true because at the time you look at it, its value has been changed by the callback function.

        alternately, use a synchronous AJAX call.

        Comment

        Working...