PHP WinBinder 3: Name Password pop-up

Revision as of 00:53, 16 July 2010 by BobS (talk | contribs) (Undo revision 4278 by Rebeca123 (Talk))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 

UniServer 5-Nano
PHP WinBinder 3.

Name Password Dialogue Box

The standard dialogue boxes are WinBinder functions currently our name-password window is an integral part of the code.

A more flexible solution in terms of PHP CLI would be to convert this code into a function.

This page provides a step-by-sep guide how to do this.

Function

Initially it sounds like a complex task however the Windows creation code is already in place.

All that is required is to place it in a function wrapper. We require a unique function name I will use mp_name_password.

function mp_name_password(){
  // Windows creation code placed here
}

 

That really is all you need to do to convert existing code into a function.

Top

Example 3 Nane Password

You need to call the function using code placed in the INIT section and wrap exiting code in a function.

Edit file

  

Results

Edit file Pro_demo_wb.php delete any lines in INIT section. Add the line shown:

//=== INIT =========================================

  wb_message_box (NULL, "Create Window", "TEST3"); 
mp_name_password(); // Create pop-up window
  wb_message_box (NULL, "Name Password closed", "TEST3");

//==================================== END INIT ====

Just below END INIT add the line shown. This is the start of our function

//=============================================================== END INIT ====

 function mp_name_password(){

Just below wb_main_loop() add a closed curly brace as shown it marks end function.

//=== 5) Enter application loop -----------------------------------------------
wb_main_loop();                                  

}// End  mp_name_password function 
  • Run the script (double click on file Pro_demo_wb.bat)
  • Result shown on the right
  • The first pop-up is just a message.
  • Once clicked the name-password window is created.
  • Enter name and password
  • Closing this Window returns to our script.
  • The last pop-up is just a message.

Important: Closing the Name-Password Window returns to our main CLI script

Note: Mechanics are in place however it’s of little use! Our script has no access to either the name or password entered.

  

 

Top

Passing Parameters back to script

You have two options of passing data back to your script. Either use a global array or global variables.

For this simple application an array is over kill, only two variables are required.

Hey! What’s all this global stuff? Well everything you define in your main script is accessible by that little pop-up window. It is a PHP function hence requires no special treatment

Top

Validation

You again have two options either to validate within your script using raw data from the pop-up or validate within the pop-up and pass clean data back to your script.

For this application it makes sense to validate within the pop-up and pass clean data back to the script.

Top

Naming convention

Another personal choice, for global variables add a prefix for example “g_” hence our two global variables are g_name and g_pass.

Having made the above choices we can implement them in our script.

Top

Add code to INIT section

Edit file Pro_demo_wb.php delete any lines in INIT section. Add the lines shown:

//=== INIT ======================================================

$g_name = "";        // Initialise variable  
$g_pass = "";        // Initialise variable  

mp_name_password(); // Create pop-up

wb_message_box (NULL, "Name =$g_name\n Password =$g_pass","TEST3"); 
print "Name =$g_name \n Password =$g_pass \n"; // command window 

 exit;
//=================================================== END INIT ====
  • First two lines create and initialise our two global variables.
  • Function mp_name_password() is run which creates our name-password pop-up.
  • Pop-up changes global variables, closing the window returns to script.
  • A message box displays name and password entered (used only for testing).
  • Note: For testing you can use the alternative and print to the command Window.
  • Finally we exit the script.

Top

Add code to Handler Function

Edit handler function as described on the right. Complete function shown belo:

//=== 4) Handler Function =====================================================

function process_main($window, $id){
 global $g_name;
 global $g_pass;

 switch($id) { 

 //--- GET USER INPUT -------------------------------------------------------=
 case  IDC_USER_ENTER:

  //-- Read user input 
  $name  = wb_get_text(wb_get_control($window, IDC_USER_NAME));   // Get name
  $name  = trim($name);
  $pass  = wb_get_text(wb_get_control($window, IDC_USER_PASSWORD));// Get pass
  $pass  = trim($pass);

  // wb_message_box ($window, $name, "TEST"); 
  // wb_message_box (NULL, $pass, "TEST");

  $g_name = $name; // Set global variable for our script to use 
  $g_pass = $pass; // Set global variable for our script to use 
 
 break; 

 //--- CLOSE ------------------------------------------------------------------
   case IDCLOSE:                        // Constant IDCLOSE (8) predefined 
    wb_destroy_window($window);         // Destroy the window
    break; 
  }
}

//==================================================== End Handler Function ===

 Modifications:

  • The handler function requires access to our two global variables these need to be specifically declared as global at the beginning of the function using the following lines:
    • global $g_name;
    • global $g_pass;
  • The two annoying message boxes have been commented out. These can be deleted however for now they remain, we may want to reinstate them for testing.
  • The following two lines are the important part of this function. They set the global variables to the name and password entered (pass data back to our script):
    • $g_name = $name; // Set global variable for our script to use
    • $g_pass = $pass; // Set global variable for our script to use

 Run Script:

  1. Run the script (double click on file Pro_demo_wb.bat)
  2. Enter a name and password
  3. Press Enter button
  4. Close Window (click cross top right)

Result:

 Name and password displayed in message box.

 On clicking OK name password printed in command window.

Top

Add validation and close Window

Although the above works there is no validation. After pressing enter it appears to do nothing.

Even worst, in order to continue executing our script a user needs to manually close the window.

The above was intentional! After all this is a step-by-step quid final step is to addess the above two issues.

Top

Add Validation

Validation is always a contentious issue you want clean data returned back to your script that can be used without further processing. For example you may want a user’s first and last names to be entered without any digits. Perhaps a two-digit password is totally inadequate and your script requires a minimum of ten digits and characters to be entered. Hence you would validate against this criteria.

Note: Never trust user input always validate.

The following example requires a user enter both a name and password this can be anything they like.

Top

Validation

//-- Ensure fields are not empty
if(!$name){
 wb_message_box($window, "Please enter a name ","Name Empty", WBC_INFO); 
 break;
}
if(!$pass){
 wb_message_box($window, "Please enter a password ","Password Empty", WBC_INFO); 
 break;
}

 

This example implements simple validation it ensures the text fiels are not empty.

  • if(!$name) if a name has not been entered
  • Display a message box (wb_message_box), informing user to enter a name
  • Break out of the case statement and wait for user input.
  • Perform same check for password.

Top

Prevent window closing

case IDCLOSE:   // Constant IDCLOSE (8) predefined 
 $text  = "Please enter a name and password\n";
 $text .= "Then press enter\n";
 $text .= "Window will automatically close";

 wb_message_box ($window, $text, "Name Password Required"); 
 // wb_destroy_window($window);      // Destroy the window
break; 

 

First issue to be addressed a user can close the window by clicking on the cross top right.

We want a user to enter a name and password and then the window to be cloded.

  • In the IDCLOSE section comment out or delete the wb_destroy_window($window)
  • Not what a user would expect hence add a message box explaining what you want a user to do.

Note: Forces a user to enter a name/password and taken through validation process.

Top

Alternative window close

$g_name = $name; //Set global variable for our script to use 
$g_pass = $pass; //Set global variable for our script to use 

wb_destroy_window($window);         // Destroy the window

 

  • After setting our global variables.
  • Close the window using
    wb_destroy_window($window);

Top

Complete handler function

Add all the code snippets to handler function. Complete function shown below:

//=== 4) Handler Function =====================================================
function process_main($window, $id){
 global $g_name;
 global $g_pass;

 switch($id) { 

 //--- GET USER INPUT -------------------------------------------------------=
 case  IDC_USER_ENTER:

  //-- Read user input 
  $name  = wb_get_text(wb_get_control($window, IDC_USER_NAME));   // Get name
  $name  = trim($name);
  $pass  = wb_get_text(wb_get_control($window, IDC_USER_PASSWORD));// Get pass
  $pass  = trim($pass);

  //-- Ensure fields are not empty
  if(!$name){
    wb_message_box($window, "Please enter a name ","Name Empty", WBC_INFO); 
    break;
  }
  if(!$pass){
   wb_message_box($window, "Please enter a password ","Password Empty", WBC_INFO); 
   break;
  }

  $g_name = $name; // Set global variable for our script to use 
  $g_pass = $pass; // Set global variable for our script to use 

  wb_destroy_window($window);         // Destroy the window
  break; 

 //--- CLOSE ------------------------------------------------------------------
   case IDCLOSE:                        // Constant IDCLOSE (8) predefined 
    $text  = "Please enter a name and password\n";
    $text .= "Then press enter\n";
    $text .= "Window will automatically close";
    wb_message_box ($window, $text, "Name Password Required"); 
   break; 
  }
}
//==================================================== End Handler Function ===

 

After modifying handler function:

  • Run the script (double click on file Pro_demo_wb.bat)
  • Enter name and password
  • Click enter.
  • Pop-up message box displays name and password.
  • Click OK
  • Name and password printed to command window.
  • Script completes and exits.


Associates files:

That completes our name password pop-up. If you cannot get it working the download includes the following two files

  • Pro_demo_2_wb.bat
  • Pro_demo_2_wb.php

These are located in folder UniServer\docs\SRC\pro_demo

Copy them to folder UniServer and run.


Note:

If your application requires additional user information add appropriate text fields as required.

Consider this code as a working template never reinvent just cut and past.

Top

Summary

Although the above code is specific to a name password pop-up techniques shown are adaptable to any pop-up you wish to create. For example you can add check boxes, radio buttons or any of the WinBinder controls.

If you find you are adding more and more controls consider creating a full WinBinder windows application.

On the previous page I mentioned the Open dialog box and Save As dialog box will not work. These require additional support covered on the next page.

Top