By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

How to validate a form using if statements

P: 4
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
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Insert players</title>
  4.  
  5. <?PHP
  6.  
  7.         //Declaring the valriable that will be used to process the form
  8.         $full_name = '';
  9.         $surname = '';
  10.         $contact_number = '';
  11.         $email = '';
  12.         $position = '';
  13.         $username = '';
  14.         $password = '';
  15.  
  16.         //These variables will hold error messages
  17.         $error_message = '';
  18.         $name_error = "";
  19.         $surname_error = "";
  20.         $contact_number_error = "";
  21.         $email_error = "";
  22.         $position_error = "";
  23.         $username_error = "";
  24.         $password_error = "";
  25.  
  26.  
  27.  
  28.     if (isset($_POST['Submit1'])) {
  29.  
  30.         //Validating input data
  31.         if($full_name == "") {
  32.  
  33.             $name_message = $name_message. "You did not enter the name";
  34.  
  35.         }
  36.             if($full_name_length <=50) {
  37.                 $full_name_length = "";
  38.             }
  39.             else {
  40.                 $full_name_length = $full_name_length. "The name muust not exceed 50 characters, and cannot be left empty";
  41.             }
  42.                 if($surname == "") {
  43.  
  44.                     $surname = $surname. "You did not enter the surname";
  45.  
  46.                 }
  47.                     if($surname_length <=50) {
  48.  
  49.                         $surname_length = "";
  50.  
  51.                     }
  52.                     else {
  53.  
  54.                         $surname_length = $surname_length. "The surname must not exceed 50 characters, and cannot be left empty";
  55.  
  56.                     }
  57.  
  58.                  else {
  59.  
  60.  
  61.  
  62.         //Posting the data entered using the POST method
  63.         $full_name = $_POST['name'];
  64.         $surname = $_POST['surname'];
  65.         $contact_number = $_POST['contact_number'];
  66.         $email = $_POST['email'];
  67.         $position = $_POST['position'];
  68.         $username = $_POST['username'];
  69.         $password = $_POST['password'];
  70.  
  71.         //Convert first letter of each word to capital letters
  72.         $full_name = ucwords($full_name);
  73.         $surname = ucwords($surname);
  74.         $position = ucwords($position);
  75.         $username = ucwords($username);
  76.  
  77.         //Checking how many characters a string contains
  78.         $full_name_length = strlen($full_name);
  79.         $surname_length = strlen($surname);
  80.         $contact_number_length = strlen($contact_number);
  81.         $position_length = strlen($position);
  82.         $username_length = strlen($username);
  83.         $password_length = strlen($password);
  84.  
  85.         //Connecting to the database
  86.         $user_name = "root";
  87.         $password = "";
  88.         $database = "zebrascricketclub";
  89.         $server = "127.0.0.1";
  90.         $db_handle = mysql_connect($server, $user_name, $password);
  91.         $db_found = mysql_select_db($database, $db_handle);
  92.  
  93.         if($db_found) {
  94.  
  95.             $query = "INSERT INTO players (name,surname,contact_number,email,position,username,password)
  96.                       VALUES ('$full_name','$surname','$contact_number','$email','$position','$username','$password')";    
  97.             $result = mysql_query($query);
  98.  
  99.             mysql_close($db_handle);
  100.             header("Location: success_insert.php");
  101.  
  102.         }
  103.         else {
  104.                 echo "Data could not be submitted";
  105.                 mysql_close($db_handle);
  106.             }
  107.  
  108.         }
  109.  
  110.  
  111.     }
  112. ?>
  113.  
  114. </head>
  115. <body>
  116.  
  117. <center><h2>Insert players in the form below</h2></center>
  118.                 <center><?php echo $name_error; ?></center>
  119.                 <center><?php echo $surname_error; ?></center>
  120. <center><FORM NAME ="form1" METHOD ="POST" ACTION ="insert.php">
  121.  
  122.     Name:    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name ='name'  value="<?PHP print $full_name; ?>" ><br>
  123.                                             <br>
  124.     Surname:    &nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'surname' value = "<?php echo $surname; ?>" ><br>
  125.                                             <br>
  126.     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>
  127.                                             <br>
  128.     Email:    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'email' value = "<?php echo $email; ?>"><br>
  129.                                             <br>
  130.     Position:    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'position' value = "<?php echo $position; ?>"><br>
  131.                                             <br>
  132.     Username:    &nbsp;&nbsp;&nbsp;&nbsp;<input type = 'text' name = 'username' value = "<?php echo $username; ?>">&nbsp;<br>
  133.                                             <br>
  134.     Password:    &nbsp;&nbsp;&nbsp;&nbsp;<input type = 'password' name = 'password' value = "<?php echo $password; ?>"><br>
  135.                             <br>
  136.     <input type = "Submit" name = "Submit1"  VALUE = "Submit"></center>
  137. </FORM>
  138.  
  139.  
  140.  
  141. </body>
  142. </html>
  143.  
  144.  
Apr 30 '13 #1
Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,364
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.
Apr 30 '13 #2

P: 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... 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.
Apr 30 '13 #3

Rabbit
Expert Mod 10K+
P: 12,364
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.
Expand|Select|Wrap|Line Numbers
  1. <?
  2. if (isset($_POST['Submit'])) {
  3.    $errormessage = '';
  4.    $username = '';
  5.    $username = $_POST['username'];
  6.  
  7.    if (strlen($username) > 50) {
  8.       $errormessage = $errormessage . 'The username is longer than fifty characters. ';
  9.    }
  10.  
  11.    if (strlen($errormessage) > 0) {
  12.       echo $errormessage;
  13.    } else {
  14.       // insert into your database
  15.    }
  16. }
  17. ?>
Apr 30 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.