worker_bee Posted December 23, 2010 Share Posted December 23, 2010 I have no problem validating a form that is straight HTML. However, I have a PHP form that does not get processed from an update form. Here is the abbreviated code that never gets validated: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title> </title> <script type="text/javascript" src="script.js"></script> <script type="text/javascript"> function validate(form){ if (form1.zip.length !== 5) { alert("You must enter a zipcode with five characters."); document.form1.zip.focus(); return false; } return true; } </script> </head> <body> <?php include ("db1.php"); $tbl_name="client"; // Table name session_start(); // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; $_SESSION['id']=$id; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $status=$rows['marital']; ?> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="form1" method="post" onsubmit="return validate(this)"></td> <table width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td colspan="4"><strong> Update Client into Database </strong></td> </tr> <tr> <td width="30%" align="right">First Name:</td> <td ><input name="firstname" type="text" id="firstname" value="<? echo $rows['firstname']; ?>"></td> </tr> <tr> <td width="30%" align="right">Last Name:</td> <td><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td> </tr> <tr> <td width="30%" align="right">Address1:</td> <td><input name="address1" type="text" id="address1" value="<? echo $rows['address1']; ?>"></td> </tr> <tr> <td width="30%" align="right">Address2:</td> <td><input name="address2" type="text" id="address2" value="<? echo $rows['address2']; ?>"></td> </tr> <tr> <td width="30%" align="right">City:</td> <td><input name="city" type="text" id="city" value="<? echo $rows['city']; ?>"></td> </tr> <tr> <td width="30%" align="right">State:</td> <td><input name="state" type="text" id="state" value="<? echo $rows['state']; ?>"></td> </tr> <tr> <td width="30%" align="right">Zip:</td> <td><input name="zip" type="text" id="zip" value="<? echo $rows['zip']; ?>"></td> </tr> <tr> <td width="30%" align="right">Country of Origin: </td> <td><input name="selcountry" type="text" id="country" value="<? echo $rows['country']; ?>"></td> </tr> <tr> <td width="30%" align="right">Birth Date:</td> <td><input name="birth" type="text" id="birth" value="<? echo $rows['birth']; ?>"></td> </tr> <tr> <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit" ></td> </tr> </table> </form> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/222501-php-forms-with-javascript-validation/ Share on other sites More sharing options...
bals28mjk Posted December 23, 2010 Share Posted December 23, 2010 The object you are getting the length for is for the input. So when you do inputObject.length it's getting the length of the input box, not the characters in the zipcode. If you want that I believe you can use form1.zip.value.length. If you are validating forms in js I advise you look into doing it with the use of regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/222501-php-forms-with-javascript-validation/#findComment-1150863 Share on other sites More sharing options...
worker_bee Posted December 23, 2010 Author Share Posted December 23, 2010 There's a typo in the code. It really should be form1.zip.length.value. It still does not get processed. Quote Link to comment https://forums.phpfreaks.com/topic/222501-php-forms-with-javascript-validation/#findComment-1150889 Share on other sites More sharing options...
bals28mjk Posted December 23, 2010 Share Posted December 23, 2010 Are you using a debugger? What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/222501-php-forms-with-javascript-validation/#findComment-1150898 Share on other sites More sharing options...
worker_bee Posted December 23, 2010 Author Share Posted December 23, 2010 I am not using a debugger. It does not seem to get to the validation function. I tried putting an alert in just to indicate that it gets to the function but nothing shows up. The enter html works fine with the same javascript. Quote Link to comment https://forums.phpfreaks.com/topic/222501-php-forms-with-javascript-validation/#findComment-1150916 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.