0

Hi all

why this code doesn't work correctly but php validate without js work correctly and js without php validate work correctly ?!

form.php




<html>
<head>
    <title>Registration Form</title>
    <script language="JavaScript" type="text/javascript">
    Function validate ()
    {
     var str=true;
document.getElementById("name").innerHTML="";
document.getElementById("username").innerHTML="";
document.getElementById("pass1").innerHTML="";
document.getElementById("pass2").innerHTML="";
document.getElementById("email1").innerHTML="";
document.getElementById("web").innerHTML="";
if(document.frm.nameTxt.value=='')
{
document.getElementById("name").innerHTML="Please Enter your name";
str=false;
}
///////////////
if(document.frm.username.value=='')
{
document.getElementById("username").innerHTML="Please Enter a Username";
str=false;
}
//////
/////////////////////////////////////////////////
if(document.frm.passTxt.value=='')
{
document.getElementById("pass1").innerHTML="Please Enter Password";
str=false;
}

if(document.frm.password.value!=document.frm.passConTxt.value)
{
document.getElementById("pass2").innerHTML="Password and Confirm Password does not match";
str=false;
}
////////////////////////////////////
var validate_char= /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(!document.frm.email.value.match(validate_char))
{
document.getElementById("email1").innerHTML="Please Enter Valid Email ID";
str=false;
}

//website
var validate_char=/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i;
if(!document.frm.website.value.match(validate_char))
{
    document.getElementById("web").innerHTML="Please Enter Valid website";
str=false;
}
return str;

    }// end validate() 

    </script>
</head>

<body>
    <center>
    <form name="frm" action= "insertData.php" method="POST" onsubmit=return "validate()" > 
        <table class="form" border="1" bgcolor="skyblue">
            <tr>
                <th><label for ="nameTxt"> Name :</label></th>

     <td><input type="text" value="" name ="nameTxt"  />
        <div id="name" style="color:#FF0000"></div>

     </td>
     </tr>
    <tr> 
        <th> <label for="userNameTxt">username</label></th>
     <td>
        <input type="text" value="" name="userNameTxt">
        <div id="username" style="color:#FF0000"></div>

     </td>
    </tr>

    <tr>
    <th><label for="password">password</label></th>
     <td>
        <input type="password" name="passTxt">
        <div id="pass1" style="color:#FF0000"></div>
        </td>
    </tr>

    <tr>
     <th><label for="passConTxt">password confirmation</label></th>
         <td>
            <input type="password" name="passConTxt" />
            <div id="pass2" style="color:#FF0000"></div>
     </td>
    </tr>

    <tr>

        <th> <label for="email">Email </label></th>
     <td>
        <input type="email" placeholder= "name@example.com" value="" name="email">
        <div id="email1" style="color:#FF0000"></div>
     </td>
     </tr>

    <tr>
        <th> <label for="Website">Website </label></th>
        <td>
        <input type="Website" placeholder="http://www.example.com" value="" id="website" 
        name="website">
        <div id="web" style="color:#FF0000"></div>
     </td>
     </tr>

    <tr>
    <td align="right"><input type="submit" value=" Submit " name="btn_done"></td>
    <td align="left"><input type="reset" value="Rest" name="btn_rst"></td>
    </tr>
    </table>
    </form>
</body>
</html>

insertData.php

<?php
// decleration for varible 
$name=$_POST['nameTxt'];
$username=$_POST['userNameTxt'];
$passTxt=$_POST['passTxt']; 
$passConTxt=$_POST['passConTxt'];
$email=$_POST['email'];
$website=$_POST['website'];
$errors=0;
$submit=$_POST['btn_done'];    
// connection to mysql 
$con=mysql_connect('localhost','root','root');
if(!$con){
    die('Could not connection '.mysql_error());
} //if $con

//select DB :
$db=mysql_select_db('reg')
or die (mysql_error());
// .....

if($submit){
    if(!isset($name) || trim($name) == ''){
        ++$errors;
    echo"Please enter the name <br>";
    }//if name

    //  username if blank :
    if(!isset($username) || trim($username) == ''){
        ++$errors;
        echo'Please enter the username <br>';
    }//if username

// email 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // echo "email address is not valid <br>";
    ++$errors;
    echo'Please enter a valid email<br>';
}// end if email    
//duplicate email
 //DETERMINE WHETHER THE EMAIL ADDRESS HAS ALREADY BEEN REGISTERED

$q = "SELECT id FROM store WHERE email = '$email'";
        $result = mysql_query ($q);
if (mysql_num_rows($result) != 0){
    ++$errors;
    echo"email is already exists<br>";}

// website :

if(!filter_var( $website,FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED))
  { ++$errors;
    echo'Please enter a valid website '.'<br>';
  }
  //end if website
  ////////////////////////////
 /*   if($_POST[] == '')
{
$err = "Please enter your password!";
}
else if (validation($_POST[confirm]) == '')
{
$err = "Please confirm your password!";
}
else if (validation($_POST[confirm]))
{
$password = $_POST[password];
$confirm = $_POST[confirm];
if ($confirm != $password) {
$err = "Your passwords does not match!<br />";
}
}*/
  ///////////////////////////
  // password :
    if (strlen($_POST['passTxt']<6)){
    ++$errors;
    echo'password should be more than 6 <br>';
  }// if password   
  //password confirmation
  elseif($_POST['passTxt']!=$_POST['passConTxt'] ){
    ++$errors;
  echo'password does not match <br>';
  }//if password confirmation

  elseif(0==$errors){
    $query="insert into store values('','$name','$username','$passTxt','$passConTxt','$email','$website')";
$data=mysql_query($query) or die(mysql_error('ooooooooooooooooooooooooooo'));
if ($data){echo "DONE ! <br>";}
  }//
}
//submit

Please suggest where i am making a mistakes! :( :( :( :'( :(

4
Contributors
6
Replies
28
Views
3 Years
Discussion Span
Last Post by aseel_1
1

Any information about what goes wrong, displayed behaviour or error messages, would help us out a little.

1

Hi,
Because you have some errors in your code:
1 <script language="JavaScript" type="text/javascript">
replace with <script type="text/javascript">

2 Function validate ()
replace with function validate()
Function isnt a keyword for javascript.

3 onsubmit=return "validate()"
replace with onsubmit="return validate()"

0

Checkout the form.php Page.
Line no 20 should be

if(document.frm.userNameTxt.value=='')

Like that you have check for every if condition.It should be like
document.formname.fieldname.value

id attribute for all input fields are missing.
Replace like this.

<input type="text" value="" name ="nameTxt" id="name" />
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.