Jump to content

PHP forms with Javascript Validation


worker_bee

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.