PDA

View Full Version : PHP forms with Javascript Validation


jnhemley
12-22-2010, 07:24 PM
I have an edit form with Javascript validation. The validation never gets executed. Please look at the following abbreviated code:
[CODE]
<!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 ("db.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 </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>

[CODE]

Philip M
12-23-2010, 08:10 PM
See:- http://www.codingforums.com/showthread.php?t=17515.

You will not get any reply in this forum which is intended to be used only to
post a completed (working) script for showcasing/benefit of others.

Before you post again please read the forum rules and posting guidelines.