View Single Post
Old 12-07-2006, 10:14 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 136
metazai is on a distinguished road
AJAX newbie needs help with form validation!

Trying to validate a form without resubmitting -- part of what I'm validating is information that's in the database. Unfortunately, it submits even when it fails the "checks". Does anyone see the obvious newbie error I am making?

Code:
<script language="JavaScript" type="text/javascript">
<!--
function createRequestObject() {  

       var req;  
     
       if(window.XMLHttpRequest){  
          // Firefox, Safari, Opera...  
          req = new XMLHttpRequest();  
       } else if(window.ActiveXObject) {  
          // Internet Explorer 5+  
          req = new ActiveXObject("Microsoft.XMLHTTP");  
       } else {  
          // There is an error creating the object,  
          // just as an old browser is being used.  
          alert('Problem creating the XMLHttpRequest object');  
       }  
     
       return req;  
     
    }  

 var http = createRequestObject();  
  
function numcheck()
{ var myurl='includes/checklogin_new.php';
 // Create rand variable to avoid cach problems
myRand=parseInt(Math.random()*999999999999999);
var modurl=myurl+"?rand="+myRand+"&firstname="+firstname+"&lastname="+lastname+"&email="+email+"&customername="+customername+"&customerpassword="+customerpassword+"&customerpassword_verify="+customerpassword_verify;
http.open("GET", modurl);
http.onreadystatechange = useHttpResponse;
http.send(null);
}

function useHttpResponse()
{ if(http.readyState == 4 && http.status == 200){ 
var errorvalue= http.responseText;
if (errorvalue) {
document.getElementById('errorcode').innerHTML=errorvalue;
return false; } else {
  document.form1.submit();
}
}}
-->
</script>
And here's the submitting form:

Code:
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
                <tr>
                  <form name="form1" method="post" action="includes/publish.php" onSubmit="javascript:return numcheck(this.form)">
                    <td align="center"></td>
                    <td align="center"><table border="0" cellpadding="3" cellspacing="0">
                        <tr>
                          <td align="right" valign="middle"><div align="right">Please Enter your First Name:</div></td>
                          <td align="left" valign="middle"><input name="firstname" type="text" id="firstname"></td>
                        </tr>
                        <tr>
                          <td align="right" valign="middle"><div align="right">Please Enter your Last Name:</div></td>
                          <td align="left" valign="middle"><input name="lastname" type="text" id="lastname"></td>
                        </tr>
                        <tr>
                          <td align="right" valign="middle"><div align="right">Please Enter Email:</div></td>
                          <td align="left" valign="middle"><input name="email" type="text" id="email"></td>
                        </tr>
                        <tr>
                          <td colspan="2" align="center" valign="middle"><span class="style1">User Name MUST be your Plan Sponsor Name</span></td>
                        </tr>
                        <tr>
                          <td align="right" valign="middle"><div align="right">Please Enter a Username:</div></td>
                          <td align="left" valign="middle"><input name="customername" type="text" id="customername"></td>
                        </tr>
                        <tr>
                          <td align="right" valign="middle"><div align="right">Please Enter a Password:</div></td>
                          <td align="left" valign="middle"><input name="customerpassword" type="password" id="customerpassword"></td>
                        </tr>
                        <tr>
                          <td><div align="right">Please Verify your Password: </div></td>
                          <td><input name="customerpassword_verify" type="password" id="customerpassword_verify"  autocomplete="off">
                          </td>
                        </tr>
                        <tr>
                          <td>&nbsp;</td>
                          <td><input type="submit" value="Create Login"></td>
                        </tr>
                      </table></td>
                  </form>
                </tr>
              </table>
              <div id="errorcode"></div>
metazai is offline   Reply With Quote