467,913 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,913 developers. It's quick & easy.

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
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
  • viewed: 6161
Share:
6 Replies
Rabbit
Expert Mod 8TB
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
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 8TB
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
64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1.  function checkExpire(form)
  2.   {
  3.     // regular expression to match required date format
  4.     re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
  5.  
  6. if(form.txtExpire.value != '' && !form.txtExpire.value.match(re)) {
  7.   alert("Invalid date format: " + form.txtExpire.value);
  8.   form.txtExpire.focus();
  9.   return false;
  10.  
  11. return true;
  12.   }
Jan 6 '21 #5
Bit
<script>
function ValidateLoginForm()
{
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.focus();
return false;
}
else{
return true;
}
}
</script>
Jan 12 '21 #6
16bit
You don't need the "else" rather, only the "if".
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. If  ($username = foo) { 
  3. echo 'username';
  4. }
  5. echo 'do something else';
  6. ?>
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. If (Case1) { 
  3. echo 'Response to case 1';
  4. } elseif (Case2) {
  5. echo 'Response to Case2';
  6. } else {
  7. echo 'response to case 3, if BOTH case1 and case2 return false';
  8. }
  9. echo 'continue code here';
  10. ?>
  11.  
BUT, we can also have:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (Case1){
  3. echo 'Response to Case1';}
  4. echo 'Response to case 2 is the continuation of the code';
  5. ?>
  6.  
Or we can have:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (Case1) {
  3. echo 'Response to Case1';
  4. } elseif (Case2) { 
  5. 'Response to Case2';
  6. }
  7. echo 'continue the code';
  8. ?>
  9.  
Finally we can have:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (case1) {
  3. echo 'response to case1';
  4. } elseif {
  5. echo 'response to case2;
  6. } else {
  7. if (Condition3) {
  8. echo 'response to condition3';
  9. } elseif (Condition4) {
  10. echo 'response to condition4';
  11. } else (Condition 5) {
  12. echo 'response to condition 5'; 
  13. }
  14. }
  15. echo 'continue script here';
  16. ?>
  17.  
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (case1) {
  3. echo 'case1 code here';
  4. exit;
  5. } elseif (case2) {
  6. ...
  7. }
  8. ...
  9. ?>
  10.  
Therefore, you would want to use something like:
Expand|Select|Wrap|Line Numbers
  1.   if($full_name_length >50) {
  2.                 echo 'error message here';
  3.                 exit;
  4.   }
  5. if ( !isset ($_POST['surname'] ) ) {
  6. echo 'error message if user did not input a surname'; 
  7. exit;
  8. }
  9.  
  10. if (some other error condition) {
  11. echo 'error message';
  12. exit;
  13. }
  14. echo 'do code here if no errors';
  15.  
1 Week Ago #7

Post your reply

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

Similar topics

2 posts views Thread by vishal | last post: by
3 posts views Thread by Justin Sane | last post: by
15 posts views Thread by simonmarkjones | last post: by
8 posts views Thread by John B | last post: by
1 post views Thread by jayparaiso | last post: by
7 posts views Thread by =?Utf-8?B?QmVu?= | last post: by
4 posts views Thread by Smokey Grindel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.