Jump to content

Form Validation


blackwolf23

Recommended Posts

Hello,

 

Its been a while since I have done any programming and I have come across a problem that I can't solve.

 

My code listed below is used to check if any fields in the Register form are empty and display a message warning the user. If the fields are not empty then the data should be inserted into the database.

 

I believe my problem lies with this line

  if ($_POST['_submit_check'] == 1)This line is naturally false when the user is first presented with the Register page and by being false my code skips straight to my Else statement.

 

I know this can be solved easily but I just can't think of a way around it at the moment.

 

Here is my code

<?php
require_once('Functions.php');
//Post Variables

   $Username = $_POST['uname'];
   $Firstname = $_POST['fname'];
   $Surname = $_POST['sname'];
   $Email= $_POST['email'];
   $Password= $_POST['password'];
   $Cpassword= $_POST['cpassword'];


//Function to connect to the database
$establish = connect();

//This is the validation code for the form fields
if ($_POST['_submit_check'] == 1)
{
     

   if(strlen($Username) < 6)
        echo 'Username must be between 6-8 characters';
    
    
    if(empty($Firstname))
     
       echo 'Firstname is a required field';
     
   
    if(empty($Surname))
     
       echo 'Surname is a required field';
     

    if(strcmp($Password,$Cpassword) != 0)
     
       echo 'Passwords do not match';
     

}

else{

//Query to insert user info into the the database 
$query = "INSERT into Users (Username,Firstname,Surname,Email,Password) 
values ('".$Username."','".$Firstname."', '".$Surname."', '".$Email."', '".$Password."' )";
$verify = mysql_query($query);



//Now redirect the user to the profile page
header('Location: Profile.html');

  

}
[\code]

Link to comment
https://forums.phpfreaks.com/topic/53562-form-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.