Jump to content

Recommended Posts

Posted

Hi all,i need help with validating form..I got 3 php files.How do i show the error messages beside the text..Here are the codes

 

output_fns.php

function do_kepala($title){
?>
"http://www.w3.org/TR/html4/strict.dtd">







};
function do_body()
{
?>



};
function do_footer()
{
?>
tes 1234   


};
function display_login_form()
{
?>

 
   
   Data keanggotaan
   

   Email
       Become your id
   
   
   Password
       6-12 karakter
   
   
   
   Ulangi Password
       6-12 karakter
   
   
   
   
   
   Personal data
   

   Name
   Your full name
   
   
   
   Birthday date
   dd/mm/yyyy
   
   
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       26
       27
       28
       29
       30
       31
     
       
        
 1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       
       

 2000
         1999
         1998
         1997
         1996
         1995
         1994
         1993
         1992
         1991
         1990
         1989
         1988
         1987
         1986
         1985
         1984
         1983
         1982
         1981
         1980
         1979
         1978
         1977
         1976
         1975
         1974
         1973
         1972
         1971
         1970
         1969
         1968
         1967
         1966
         1965
         1964
         1963
         1962
         1961
         1960
       
       
       Sex
Jenis kelamin anda

       
Choose one... 
male
female

       
       
       Address
       Your address
       
       
       
       Kota
       Town
       
       
       
       Propinsi
       Province
       
       
       Pilih salah satu....
Nanggroe Aceh Darussalam
Sumatera Utara
Sumatera Barat
Bengkulu
Riau
Kepulauan Riau
Jambi
Sumatera Selatan
Lampung
Kepulauan Bangka Belitung
DKI Jakarta
Jawa Barat
Banten
Jawa Tengah
DI Yogyakarta
Jawa Timur
Kalimantan Barat
Kalimantan Tengah
Kalimantan Selatan
Kalimantan Timur
Bali
Nusa Tenggara Barat
Nusa Tenggara Timur
Sulawesi Barat
Sulawesi Utara
Sulawesi Tengah
Sulawesi Selatan
Sulawesi Tenggara
Gorontalo
Maluku
Maluku Utara
Papua Barat
Papua
       

   

   Sign-up
  
 


};
?>

 

reg.php

$email=$_POST['email'];
$pwd1=$_POST['pwd1'];
$pwd2=$_POST['pwd2'];
$tgl=$_POST['tgl'];
$bln=$_POST['bln'];
$thn=$_POST['thn'];
$tgl_lahir=$tgl.'-'.$bln.'-'.$thn;
$jekel=$_POST['jekel'];
$alamat=$_POST['adrress'];
$kota=$_POST['town'];
$propinsi=$_POST['province'];
?>

 

register.php

include('output_fns.php');
do_kepala('Register');
do_body();
display_login_form();
?>

Posted

Not quite sure what you are asking...

 

Are you wanting to verify each field on the register.php page to make sure, for example, they enter a valid email address, or the password is 6-12 characters long?

Posted (edited)

Hi,

 

Firstly could you help us to help you:

    [*]Be more specific in your question (eg. "I'd like to check the given data for validity and display warning messages next to the specific fields if the data is not valid (eg if it's too long or contains special characters). Any thoughts on how to do this?" )

    [*]Use

     tags when posting php code to improve readability.
    [*]Put a line break before each function to improve readability.
    [*]Include a brief description of the files (eg "this one does the outputting", "this one grabs the data without any sort of safety checks but I think the checking should probably be done in here?", "this one ties it all together") or at least some comments in the code.
    [/list]
    Grumble done. Moving on to the answer part:
    
    Now assuming that we've understood you correctly, set register.php to include the reg file:
    [php]
    include('output_fns.php');
    do_kepala('Register');
    ...
    ?>
    

     

    Get the reg file to do some validation and build a list of error messages when it encounters problems:

    // only check things if we've got input to check
    $errors = array();
    if( isset($_POST['submit'] ) {
     // check email
     $email=$_POST['email'];
     // email regex courtesy of http://www.regular-expressions.info/email.html
     if( !preg_match( '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email )
     {
       $email = '';
       $errors['email'] = 'Email is not valid';
     }
     
     // check passwords
     $pwd1=$_POST['pwd1'];
     $pwd2=$_POST['pwd2'];
     if( $pwd1 == '' ) {
       $errors['pwd1l'] = 'First password field not filled in';
     }
     if( $pwd2 == '' ) {
       $errors['pwd1l'] = 'Second password field not filled in';
     }
     elseif( $pwd1 != $pwd2 ) {
       $pwd1 = '';
       $pwd2 = '';
       $errors['pwd2'] = 'Passwords do not match';
     }
     
     // and so on and so on checking each field and setting appropriate error messages
    }
    ?>
    

     

    Then finally get your login form to output the errors in their right places and with some sort of formatting. In order to avoid massively polluting your output function let's create another function to show each of the errors when called.

    so, add:

    /**
    * Displays an error message
    * @param $field string  The field whose error message is to be displayed
    */
    function doError( $field )
    {
     if( isset($errors[$field]) ) {
       echo ' '.$errors[$field].'
     }
    }
    

    (of course you'll need to add a style rule to set the display of class "error". Something like:

    <br />
    .error {<br />
      color: red;<br />
      font-weight: bold;<br />
      font-size: 0.9em;<br />
    }<br />
    
    

    in the

    of your document or just add the rule to your existing stylesheet if you have one.

     

    Now we call that function each time we show an input:

    /**
    * Displays the login form with error messages if any have been generated
    */
    function display_login_form()
    {
    ?>
    
     
       
       Data keanggotaan
       
    
       Email
           Become your id
       
       
       
       Password
           6-12 karakter
       
       
       
       
       Ulangi Password
           6-12 karakter
       
       
       
       
       // etc, etc, ...
    ?>
    

     

    This code has not been tested. The checks you choose to employ to validate your data are your own concern and I do not guarantee that anything here is exactly what you need. Hopefully it'll help you a long way towards where you need to be.

     

    Hello

    :-Dave

     

    P.S. You may want to consider putting the values last posted in the fields as default values when the page reloads otherwise the user will have to re-enter their data. For the text fields this is pretty straightforward (add value="<?php echo htmlspecialchars($email); ?>" for example to the input field). For the option lists it's a bit more involved but i'm going to sleep now so I'll let you figure that out for yourself.

Edited by lightinthedark
  • Thanks 1
Posted

beat me to it, but a great answer

 

all I was going to say was:

 

in the reg.php file for the email verification, change the $email entry to something like

$email = $_POST['email'];
if (!strchr($email, '@')) {
echo "Sorry, that isn't a valid email address.";
}

  • Thanks 1
Posted
Thanks a lot guys..I'll try to implement it and sorry if my posting is not the way it should be..What i'm trying to do is to validate the email field,etc.For example if the email field is left blank,then it will display errors...
Posted

Hi lightinthedark,i've followed as you says..On the register.php displays

 

Notice: Undefined index: email in C:\wamp\www\Rekan Doa\reg.php on line 5

 

here are the codes that i modified.Did i made mistakes?Should it be display the register.php instead of reg.php when i click submit?By the way i include the files

 

//Holds the function to be used for display the pages

output_fns.php

function do_kepala($title){
?>

"http://www.w3.org/TR/html4/strict.dtd">





<br />
.error {<br />
  color: red;<br />
  font-weight: bold;<br />
  font-size: 0.9em;<br />
}<br />




};
function do_body()
{
?>



};
function do_footer()
{
?>
   


};
function display_login_form()
{
?>

 
   
   Data keanggotaan
   

   Email anda
       Jadi login id anda
   
   
   
   Password
       6-12 karakter
   
   
   
   Ulangi Password
       6-12 karakter
   
   
   
   
   
   Data pribadi
   

   Nama 
   Nama lengkap anda
   
   
   
   Tanggal lahir
   dd/mm/yyyy
   
   
       1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       13
       14
       15
       16
       17
       18
       19
       20
       21
       22
       23
       24
       25
       26
       27
       28
       29
       30
       31
     
       
         
  1
       2
       3
       4
       5
       6
       7
       8
       9
       10
       11
       12
       
       

  2000
         1999
         1998
         1997
         1996
         1995
         1994
         1993
         1992
         1991
         1990
         1989
         1988
         1987
         1986
         1985
         1984
         1983
         1982
         1981
         1980
         1979
         1978
         1977
         1976
         1975
         1974
         1973
         1972
         1971
         1970
         1969
         1968
         1967
         1966
         1965
         1964
         1963
         1962
         1961
         1960
       
       
       Jenis Kelamin
Jenis kelamin anda

       
 Pilih salah satu... 
 Laki-laki
 Perempuan
 
       
       
       Alamat 
       Alamat lengkap anda
       
       
       
       Kota 
       Kota anda
       
       
       
       Propinsi 
       Propinsi anda
       
       
       Pilih salah satu....
 Nanggroe Aceh Darussalam
Sumatera Utara
Sumatera Barat
Bengkulu
Riau
Kepulauan Riau
Jambi
Sumatera Selatan
Lampung
Kepulauan Bangka Belitung
DKI Jakarta
Jawa Barat
Banten
Jawa Tengah
DI Yogyakarta
Jawa Timur
Kalimantan Barat
Kalimantan Tengah
Kalimantan Selatan
Kalimantan Timur
Bali
Nusa Tenggara Barat
Nusa Tenggara Timur
Sulawesi Barat
Sulawesi Utara
Sulawesi Tengah
Sulawesi Selatan
Sulawesi Tenggara
Gorontalo
Maluku
Maluku Utara
Papua Barat
Papua
       

       
   

   
   Sign-up
    

};
//the code that you gave me
function doError( $field )
{
 if( isset($errors[$field]) ) {
   echo ''.$errors[$field].'';
 }
};
?>

 

//Used to validate values and save to database

reg.php

 


include("db.inc.php");

$email=$_POST['email'];
$pwd1=$_POST['pwd1'];
$pwd2=$_POST['pwd2'];
$tgl=$_POST['tgl'];
$bln=$_POST['bln'];
$thn=$_POST['thn'];
$tgl_lahir=$tgl.'-'.$bln.'-'.$thn;
$jekel=$_POST['jekel'];
$alamat=$_POST['alamat'];
$kota=$_POST['kota'];
$propinsi=$_POST['propinsi'];

//this one is from RabbieBurns
$errors = array();
if( isset($_POST['submit'] )) {
 // check email
 $email = $_POST['email'];
if (!strchr($email, '@')) {
echo "Sorry, that isn't a valid email address.";
}  
 
 // check passwords
 $pwd1=$_POST['pwd1'];
 $pwd2=$_POST['pwd2'];
 if( $pwd1 == '' ) {
   $errors['pwd1l'] = 'First password field not filled in';
 }
 if( $pwd2 == '' ) {
   $errors['pwd1l'] = 'Second password field not filled in';
 }
 elseif( $pwd1 != $pwd2 ) {
   $pwd1 = '';
   $pwd2 = '';
   $errors['pwd2'] = 'Passwords do not match';
 }
 
 // and so on and so on checking each field and setting appropriate error messages
}

?>

 

//display the registration form

register.php

include('output_fns.php');
include('reg.php');
do_kepala('Register');
do_body();
display_login_form();
?>

Rekan Doa.zip

Posted (edited)

The output there is only a notice, so it looks like you've got php set to be very strict. That's no problem, but be prepared for more, similar things as you go on.

The warning says that the index 'email' isn't defined on line 5 of reg.php . Looking at that line I see it's where you assign the value of $_POST['email'] to your variable. I also notice that in the code given you're assigning to that variable twice (once in your original block, once in the block from my suggestion) which is un-necessary. To solve problem 1 (invalid index) i suggest using a ternary if (see "ternary operator" on this page ). For problem 2 (assigning twice), i suggest removing the block before the stuff I gave you. That ends up with:

 

include("db.inc.php");

$errors = array();
if( isset($_POST['submit'] )) {
 // check email
 // (this one is from RabbieBurns)
 $email = ( isset($_POST['email']) ? $_POST['email'] : '' );
 if (!strchr($email, '@')) {
   echo "Sorry, that isn't a valid email address.";
 }  
 
 // check passwords
 $pwd1=( isset($_POST['pwd1']) ? $_POST['pwd1'] : '' );
 $pwd2=( isset($_POST['pwd2']) ? $_POST['pwd2'] : '' );
 if( $pwd1 == '' ) {
   $errors['pwd1l'] = 'First password field not filled in';
 }
 if( $pwd2 == '' ) {
   $errors['pwd1l'] = 'Second password field not filled in';
 }
 elseif( $pwd1 != $pwd2 ) {
   $pwd1 = '';
   $pwd2 = '';
   $errors['pwd2'] = 'Passwords do not match';
 }
 
 // and so on and so on checking each field and setting appropriate error messages
}

?>

 

As for

Should it be display the register.php instead of reg.php when i click submit?

Yes. kinda. I had assumed that that's what it was doing (didn't read your code as thoroughly as I might have done). If you want to go to reg instead of register then reg is going to have to call the same page-displaying stuff that register does in order to get the form to re-display with error messages. I had thought reg.php was going to be just a "process the data" file rather than doing any display or other stuff. It's up to you how you put the pieces together really, but everything you need (i think) is there (subject to debugging and moulding to your specific plans and needs).

 

As I said before, the code isn't tested and will probably need a bit of adjustment to be exactly what you need. I'm just trying to point you in what seems like a good direction.

 

BTW, sorry if I was a bit grumpy on my first reply where I criticised your original request style. It was very late at night and my diplomacy/tact circuits weren't running at full power. :-)

 

Hello

:-Dave

Edited by lightinthedark
  • Thanks 1
Posted
Thanks Dave,i got more knowledge here..Btw,that's ok Dave about your criticism..I learn how to post the way it should be so other people can understand what kind of help that i ask for. :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...