PHP WinBinder 3: Name Password pop-up: Difference between revisions
(New page: {{Nav 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 mor...) |
(No difference)
|
Revision as of 20:01, 6 April 2010
PHP WinBinder 3 : Introduction | Dialog pop-ups | Name Password pop-up | Open and Save As dialog boxes
|
|
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. |
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
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. |
|
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
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.
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.
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 ==== |
|
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:
Run Script:
Result: Name and password displayed in message box. On clicking OK name password printed in command window. |
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.
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.
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.
|
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.
Note: Forces a user to enter a name/password and taken through validation process. |
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 |
|
|
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:
That completes our name password pop-up. If you cannot get it working the download includes the following two files
These are located in folder UniServer\docs\SRC\pro_demo Copy them to folder UniServer and run.
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. |
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.