How to validate a form using if statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RodPhp5
    New Member
    • Apr 2013
    • 4

    How to validate a form using if statements

    Please i need help on the code i have posted below. I am supposed to validate the form before posting the data into the database. When i run the code on my computer it is submiting all the data into the database, but the password is not submited and before submiting the data for any missing fields the user must be notified. That is what i am trying to do with the if statements. Pleae help its urgent
    Code:
    <html>
    <head>
    <title>Insert players</title>
    
    <?PHP
    	
    		//Declaring the valriable that will be used to process the form
    		$full_name = '';
    		$surname = '';
    		$contact_number = '';
    		$email = '';
    		$position = '';
    		$username = '';
    		$password = '';
    		
    		//These variables will hold error messages
    		$error_message = '';
    		$name_error = "";
    		$surname_error = "";
    		$contact_number_error = "";
    		$email_error = "";
    		$position_error = "";
    		$username_error = "";
    		$password_error = "";
    		
    	
    
    	if (isset($_POST['Submit1'])) {
    	
    		//Validating input data
    		if($full_name == "") {
    		
    			$name_message = $name_message. "You did not enter the name";
    		
    		}
    			if($full_name_length <=50) {
    				$full_name_length = "";
    			}
    			else {
    				$full_name_length = $full_name_length. "The name muust not exceed 50 characters, and cannot be left empty";
    			}
    				if($surname == "") {
    				
    					$surname = $surname. "You did not enter the surname";
    				
    				}
    					if($surname_length <=50) {
    					
    						$surname_length = "";
    					
    					}
    					else {
    						
    						$surname_length = $surname_length. "The surname must not exceed 50 characters, and cannot be left empty";
    						
    					}
    					
    				 else {
    
    		
    	
    		//Posting the data entered using the POST method
    		$full_name = $_POST['name'];
    		$surname = $_POST['surname'];
    		$contact_number = $_POST['contact_number'];
    		$email = $_POST['email'];
    		$position = $_POST['position'];
    		$username = $_POST['username'];
    		$password = $_POST['password'];
    		
    		//Convert first letter of each word to capital letters
    		$full_name = ucwords($full_name);
    		$surname = ucwords($surname);
    		$position = ucwords($position);
    		$username = ucwords($username);
    		
    		//Checking how many characters a string contains
    		$full_name_length = strlen($full_name);
    		$surname_length = strlen($surname);
    		$contact_number_length = strlen($contact_number);
    		$position_length = strlen($position);
    		$username_length = strlen($username);
    		$password_length = strlen($password);
    		
    		//Connecting to the database
    		$user_name = "root";
    		$password = "";
    		$database = "zebrascricketclub";
    		$server = "127.0.0.1";
    		$db_handle = mysql_connect($server, $user_name, $password);
    		$db_found = mysql_select_db($database, $db_handle);
    		
    		if($db_found) {
    				
    			$query = "INSERT INTO players (name,surname,contact_number,email,position,username,password)
    					  VALUES ('$full_name','$surname','$contact_number','$email','$position','$username','$password')";	
    			$result = mysql_query($query);
    			
    			mysql_close($db_handle);
    			header("Location: success_insert.php");
    			
    		}
    		else {
    				echo "Data could not be submitted";
    				mysql_close($db_handle);
    			}
    		
    		}
    		
    
    	}
    ?>
    
    </head>
    <body>
    
    <center><h2>Insert players in the form below</h2></center>
    				<center><?php echo $name_error; ?></center>
    				<center><?php echo $surname_error; ?></center>
    <center><FORM NAME ="form1" METHOD ="POST" ACTION ="insert.php">
    	
    	Name:	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name ='name'  value="<?PHP print $full_name; ?>" ><br>
    											<br>
    	Surname:	&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'surname' value = "<?php echo $surname; ?>" ><br>
    											<br>
    	Contact number:	&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'contact_number' value = "<?php echo $contact_number; ?>">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
    											<br>
    	Email:	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'email' value = "<?php echo $email; ?>"><br>
    											<br>
    	Position:	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'position' value = "<?php echo $position; ?>"><br>
    											<br>
    	Username:	&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'username' value = "<?php echo $username; ?>">&nbsp;<br>
    											<br>
    	Password:	&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'password' name = 'password' value = "<?php echo $password; ?>"><br>
    							<br>
    	<input type = "Submit" name = "Submit1"  VALUE = "Submit"></center>
    </FORM>
    
    
    
    </body>
    </html>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't understand your question.

    but the password is not submited
    What password? What does that have to do with anything?

    before submiting the data for any missing fields the user must be notified
    I don't see anywhere in your code where you notify the user.

    Also, the code logic doesn't make any sense. You're checking values before any of the values are populated. You've also either split your check from the insert code so they don't overlap or you have a phantom else. It's difficult to parse out your use of brackets.

    Comment

    • RodPhp5
      New Member
      • Apr 2013
      • 4

      #3
      When i say the password is not submited i mean, when i check the database for the data that i inserted using the form, everything is there except the password... I try to notify the user by using those if statements. Or if maybe you can just show me a sample logic on how i can validate my form before posting the data into the database.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        When i say the password is not submited i mean, when i check the database for the data that i inserted using the form, everything is there except the password...
        Okay, but what does that have to do with the question? I don't understand the relevance.

        I try to notify the user by using those if statements.
        Except that I don't see any notification anywhere. All you're doing is populating variables but not doing anything with them.

        Or if maybe you can just show me a sample logic on how i can validate my form before posting the data into the database.
        Here's a quick example. It's off the top of my head so it may not be syntactically correct but the logic is there.
        Code:
        <?
        if (isset($_POST['Submit'])) {
           $errormessage = '';
           $username = '';
           $username = $_POST['username'];
        
           if (strlen($username) > 50) {
              $errormessage = $errormessage . 'The username is longer than fifty characters. ';
           }
        
           if (strlen($errormessage) > 0) {
              echo $errormessage;
           } else {
              // insert into your database
           }
        }
        ?>

        Comment

        • Sherin
          New Member
          • Jan 2020
          • 77

          #5
          Try This Code

          Code:
           function checkExpire(form)
            {
              // regular expression to match required date format
              re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
          
          if(form.txtExpire.value != '' && !form.txtExpire.value.match(re)) {
            alert("Invalid date format: " + form.txtExpire.value);
            form.txtExpire.focus();
            return false;
          } 
          
          return true;
            }

          Comment

          • williamryeager
            New Member
            • Jan 2021
            • 1

            #6
            <script>
            function ValidateLoginFo rm()
            {
            var username = document.form1. username;
            var password = document.form1. password;
            var email = document.form1. email;



            if (username.value == "")
            {
            alert("Your username wasn't recognised.");
            username.focus( );
            return false;
            }

            if (password.value == "")
            {
            alert("Please enter a password.");
            password.focus( );
            return false;
            }
            if (email.value == "")
            {
            alert("Please enter your email address.");
            mypassword.focu s();
            return false;
            }
            else{
            return true;
            }
            }
            </script>

            Comment

            • bakertaylor28
              New Member
              • Feb 2021
              • 45

              #7
              You don't need the "else" rather, only the "if".
              Code:
              <?php
              If  ($username = foo) { 
              echo 'username';
              }
              echo 'do something else';
              ?>
              will return username if username = foo, and will return do something else if username = anything else. "else" or "elseif" is not required, unless there are three or more cases. The way if cases work is like this:

              Code:
              <?php
              If (Case1) { 
              echo 'Response to case 1';
              } elseif (Case2) {
              echo 'Response to Case2';
              } else {
              echo 'response to case 3, if BOTH case1 and case2 return false';
              }
              echo 'continue code here';
              ?>
              BUT, we can also have:
              Code:
              <?php
              if (Case1){
              echo 'Response to Case1';}
              echo 'Response to case 2 is the continuation of the code';
              ?>
              Or we can have:
              Code:
              <?php
              if (Case1) {
              echo 'Response to Case1';
              } elseif (Case2) { 
              'Response to Case2';
              }
              echo 'continue the code';
              ?>
              Finally we can have:

              Code:
              <?php
              if (case1) {
              echo 'response to case1';
              } elseif {
              echo 'response to case2;
              } else {
              if (Condition3) {
              echo 'response to condition3';
              } elseif (Condition4) {
              echo 'response to condition4';
              } else (Condition 5) {
              echo 'response to condition 5'; 
              }
              }
              echo 'continue script here';
              ?>
              The limitation being that case2 must return false before the script will evaluate for conditions 3, 4, or 5.
              The way we can make sure that the script exits after each case is to include the following as the last line of each case:

              Code:
              <?php
              if (case1) {
              echo 'case1 code here';
              exit;
              } elseif (case2) {
              ...
              }
              ...
              ?>
              Therefore, you would want to use something like:
              Code:
                if($full_name_length >50) {
                              echo 'error message here';
                              exit;
                }
              if ( !isset ($_POST['surname'] ) ) {
              echo 'error message if user did not input a surname'; 
              exit;
              }
              
              if (some other error condition) {
              echo 'error message';
              exit;
              }
              echo 'do code here if no errors';
              Last edited by bakertaylor28; Feb 25 '21, 02:31 AM. Reason: clarification

              Comment

              Working...