Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP forms with Javascript Validation

    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]

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •