Problem with validation in code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kyal
Forum Newbie
Posts: 2
Joined: Wed Mar 15, 2006 8:58 am

Problem with validation in code

Post by kyal »

Pimptastic | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi I have created a script that displays a form, takes information and enters the staff details into a mysql database this all works fine except that the validation that is supposed to check that there are no blank fields or letters in the phone number ect. Seems to execute when the page is initially loaded instead of when the submit button is pressed. When the form loads it immediately says that there are blank field that must be filled and if there are blank field when submit is pressed there blank field is entered and not stopped by the warning message. Does anyone know of a solution to get this section of code to execute only once the submit button is pressed?

Thanks for any help you can give much appreciated.
The code is shown below:

Code: Select all

<?php
  /*Staff_details.php
   *A Program to collect the staff details and enter them into the
   *database using the Entry.php script.
   */
?>
<html>
<head><title>Staff Details Entry</title></head>
<body>
<?php

  $firstname = strip_tags(trim($_POST['firstname']));
  $lastname = strip_tags(trim($_POST['lastname']));
  $department = strip_tags(trim($_POST['department']));
  $roomnumber = strip_tags(trim($_POST['roomnumber']));
  $phonenumber = strip_tags(trim($_POST['phonenumber']));

  /* set up array of field labels */
  $label_array = array ( 
                         "firstname" => "First Name",
                         "lastname" => "Last Name",
                         "department" => "Department",
                         "roomnumber" => "Room Number",
                         "phonenumber" => "Phone Number");

   foreach ($_POST as $field => $value);

{
    /* check each field for blank fields */
    if ( $value == "" )
    {
        $blank_array[$field] = "blank";
    }
    elseif ( ereg("(lastname)",$field) )  
    {
      if (!ereg("^[A-Za-z' -]{150}$",$_POST[$field]) )
      {
             $bad_format[$field] = "bad";
      }
    }
    elseif ($field == "phonenumber")
    {
      if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
      {
             $bad_format[$field] = "bad";
      }
    }
  } // end of foreach for $_POST
  /* if any fields were not okay, display error message and form */
  if (@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
  {
    if (@sizeof($blank_array) > 0)
    {
        /* display message for missing information */
        echo "<b>You didn't fill in one or more required fields. 
                 You must enter:</b><br>";
        /* display list of missing information */
        foreach($blank_array as $field => $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
        }
    }
    if (@sizeof($bad_format) > 0)
    {
        /* display message for bad information */
        echo "<b>One or more fields have information that appears to 
                 be incorrect. Correct the format for:</b><br>";
        /* display list of bad information */
        foreach($bad_format as $field => $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
        }
    }

   echo "<p><hr>
      <form action='entry.php' method='POST'>
      <center>
      <table width='95%' border='0' cellspacing='0' cellpadding='2'>
      </tr>

      <tr><td align='right'><B>{$label_array['firstname']}:</br></td>
        <td><input type='text' name='firstname' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['lastname']}:</B></td>
        <td> <input type='text' name='lastname' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['department']}:</B></td>
        <td> <input type='text' name='department' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['roomnumber']}:</B></td>
        <td> <input type='text' name='roomnumber' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['phonenumber']}:</B></td>
        <td> <input type='text' name='phonenumber' size='65' maxlength='65'> </td>
      </tr>

      </table>
      <p><input type='submit' value='Enter staff details'>
      </form>
      </center>";
  
  }
?>
</body></html>
:x :x


Pimptastic | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SERVER['REQUEST_METHOD'] being 'POST'
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Maybe something like:

Code: Select all

<?php
  /*Staff_details.php
   *A Program to collect the staff details and enter them into the
   *database using the Entry.php script.
   */
?>
<html>
<head><title>Staff Details Entry</title></head>
<body>
<?php

  $firstname = strip_tags(trim($_POST['firstname']));
  $lastname = strip_tags(trim($_POST['lastname']));
  $department = strip_tags(trim($_POST['department']));
  $roomnumber = strip_tags(trim($_POST['roomnumber']));
  $phonenumber = strip_tags(trim($_POST['phonenumber']));

  /* set up array of field labels */
  $label_array = array ( 
                         "firstname" => "First Name",
                         "lastname" => "Last Name",
                         "department" => "Department",
                         "roomnumber" => "Room Number",
                         "phonenumber" => "Phone Number");

if ($_POST['is_submitted'] == 'yes']) {
   foreach ($_POST as $field => $value);

{
    /* check each field for blank fields */
    if ( $value == "" )
    {
        $blank_array[$field] = "blank";
    }
    elseif ( ereg("(lastname)",$field) )  
    {
      if (!ereg("^[A-Za-z' -]{150}$",$_POST[$field]) )
      {
             $bad_format[$field] = "bad";
      }
    }
    elseif ($field == "phonenumber")
    {
      if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
      {
             $bad_format[$field] = "bad";
      }
    }
  } // end of foreach for $_POST
  /* if any fields were not okay, display error message and form */
  if (@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
  {
    if (@sizeof($blank_array) > 0)
    {
        /* display message for missing information */
        echo "<b>You didn't fill in one or more required fields. 
                 You must enter:</b><br>";
        /* display list of missing information */
        foreach($blank_array as $field => $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
        }
    }
    if (@sizeof($bad_format) > 0)
    {
        /* display message for bad information */
        echo "<b>One or more fields have information that appears to 
                 be incorrect. Correct the format for:</b><br>";
        /* display list of bad information */
        foreach($bad_format as $field => $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
        }
    }
}

   echo "<p><hr>
      <form action='entry.php' method='POST'>
      <input type='hidden' name='is_submitted' value='yes'>
      <center>
      <table width='95%' border='0' cellspacing='0' cellpadding='2'>
      </tr>

      <tr><td align='right'><B>{$label_array['firstname']}:</br></td>
        <td><input type='text' name='firstname' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['lastname']}:</B></td>
        <td> <input type='text' name='lastname' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['department']}:</B></td>
        <td> <input type='text' name='department' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['roomnumber']}:</B></td>
        <td> <input type='text' name='roomnumber' size='65' maxlength='65'> </td>
      </tr>

      <tr><td align='right'><B>{$label_array['phonenumber']}:</B></td>
        <td> <input type='text' name='phonenumber' size='65' maxlength='65'> </td>
      </tr>

      </table>
      <p><input type='submit' value='Enter staff details'>
      </form>
      </center>";
  
?>
</body></html>
I use a hidden field because IE does not pass the submit input if the user uses Enter rather than the Submit button.
(#10850)
kyal
Forum Newbie
Posts: 2
Joined: Wed Mar 15, 2006 8:58 am

Thanks but not sure what you mean

Post by kyal »

Hi feyd, thank for the post im not too sure how the code is meant to be implemented, I have tried a few things but have not been able to get it working sorry, I have only been working with php about 6weeks.

Thanks for any further info you can give me.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

i think he might have meant what arbornit was trying to say.
Post Reply