tags:

views:

457

answers:

2

I have created an form validation using ajax/php. Each text box is validated using a different file e.g. username_ajax.php. Once the information has been checked to be ok it is then echoed on screen ("Username ok"). I cannot figure out how to allow the user to only press the submit button once all text boxes are 'ok'. Any help will be appreciated, Thanks.

My code (Sorry its long):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sign-up</title>
</head>
<script type="text/javascript">

   function username(username)
{
    var httpRequest;
    make_request()
    function stateck() 
        {
            if(httpxml.readyState==4)
            {
            document.getElementById    ("user_div").innerHTML=httpxml.responseText;
            }
        } 

    httpxml.onreadystatechange=stateck;
    user_url="ajax_username.php?username=" + username.value;
    httpxml.open("GET",user_url,true);
    httpxml.send(null);
}


function email(email)
{
    var httpRequest;
        make_request()
        function stateck() 
        {
            if(httpxml.readyState==4)
            {
            document.getElementById("email_div").innerHTML=httpxml.responseText;
            }
        }

    httpxml.onreadystatechange=stateck;
    email_url="ajax_email.php?value=" + email.value;
    httpxml.open("GET",email_url,true);
    httpxml.send(null);
}

function confirm(password,conpassword)

{
    var httpRequest;
    make_request()
    function stateck()
    {
            if(httpxml.readyState==4)
            {
            document.getElementById("conpass_div").innerHTML=httpxml.responseText;
            }
        }
    httpxml.onreadystatechange=stateck;
    conpassword_url="ajax_password.php?password=" + password.value + "&conpassword=" + conpassword.value;
    httpxml.open("GET",conpassword_url,true);
     httpxml.send(null);
}


</script>

<body>
<div id="user_div"></div>
<div id="conpass_div"></div>
<div id="email_div"></div>
<form id="form" name="form" method="post" action="">
<label>Username<br />
      <input type="text" name="username" id="username" onBlur="check_username(username)">
       <br />
<br />
    </label>

  <label>Password<br />
    <input type="password" name="password" id="password" onBlur="check_confirm     (password,conpassword);">
   </label>    

  <label><br />
    <br />
    Confirm Password<br />
    <input type="password" name="conpassword" id="conpassword" onBlur="check_confirm(password,conpassword);">
   <br />
   <br />
  </label>    

<label>Email<br />
<input type="text" name="email" id="email" onblur="check_email(email)" />
  <p>
  <p>
  <label>
         <input type="submit" name="button" id="button" value="Submit" onclick="return false;" />
       </label>
    </p>
</form>
</body>
</html>
A: 

Set the submit button to disabled until all of the inputs have been validated.

To do this check, when done checking any input, check to see if all other inputs have been validated already. You'll need to create a global array to hold which inputs are valid.

Note that even though you're doing this validation you should also re-do the validation once the form is actually submitted since a malicious user could still send invalid input data.

Ben S
Thanks I have already tried using the global array...such as added a one each time its validated. Then doing an if statement if the array equals four. But I dont know how to 'enable' the submit button so the page doesnt refresh showing the error?
Elliott
A: 

You need to have status flags and a check routine..

Start out with your input button disabled:

<input type="submit" name="submit" id="button" disabled>

Basically..

var userNameOk;
var passwordOk;
var emailOk;

function checkCanSubmit() {
  if (userNameOk && passwordOk && emailOk) {
    document.getElementById("button").disabled= false;
  else
    document.getElementById("button").disabled= true;
}

Then you would modify your anonymous methods for each check function to update the proper status flag and call the checkCanSubmit() method;

function stateck() 
    {
            if(httpxml.readyState==4)
            {
              if (httpxml.response.Text == "whateverisvalid") {
                emailOk = true;
              } else {
                emailOk = false;
              }
              checkCanSubmit();
              document.getElementById("email_div").innerHTML=httpxml.responseText;
            }
    }

BTW, I would question the fact that you're sending the username and password.. on the query string.. for all the world to see.. Which will be stored (perhaps forever) an any number of router logs, snifferes, proxy servers.. and your web servers log files.

Is there not a better / cleaner / smarter way to do that?

datacop
Thanks I Will try thatand yes about the secuirty....I am going to encrypt the information sent. This isn't a finished script yet. Thamks
Elliott
Just one more thingHow would I have the button disabled by default? such as when the page loads...?
Elliott
I modified my answer to show how to markup the input button to be disabled and fixed the checkCanSubmit().. enjoy :)
datacop
Thanks that worksBut I keep getting an error:Message: 'httpxml.response.Text' is null or not an object
Elliott
Fixed it should be responseText not response.Text :D
Elliott