ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Using AJAX within JQUERY to run PHP that calls and returns data from RPG

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using AJAX within JQUERY to run PHP that calls and returns data from RPG

    the start the JQUERY & AJAX call:

    $pf_scriptname - Current page (php)

    PHP Code:
      <script type="text/javascript">
    jQuery(document).ready( function()
    {
    // Bind event to the Back button
    jQuery("#username").focus();

    jQuery("#memberlogin").hide();

    jQuery("#guestlogin").click(function() {
    $(
    theform.submitbutton.value 'Create Guest Account');
    jQuery("#guestlogintable").show('slow');
    jQuery("#ibmlogin").hide('slow');
    jQuery(this).hide();
    jQuery("#memberlogin").show('slow');
    });

    jQuery("#memberlogin").click(function() {
    $(
    theform.submitbutton.value 'Login');
    jQuery("#guestlogintable").hide('slow');
    jQuery("#ibmlogin").show('slow');
    jQuery(this).hide();
    jQuery("#memberlogin").hide('slow');
    jQuery("#guestlogin").show();
    });


    $(
    "input").click( function() {
    var 
    userdata "task=trylogin&memberusername=" + $("#username").val() + "&memberpassword=" + $("#password").val();
    var 
    userurl "http://10.0.1.1:8150/wsphp/$pf_scriptname";

    // reset the error fields
    //var $data = "";
    //$("#error1 span").text("Invalid userid");
    //$("[name=memberusername]").css("background-color","pink"); 

    // call to program to validate form
    $.ajax({
    url"$pf_scriptname",
    datauserdata,
    type"POST",
    success: function(data) {


    var 
    returndata data;
    var 
    error1 returndata.substring(01),
    error2 returndata.substring(12);


    //alert('Returned ' + error1 + ' cc ' + error2 + 'reload: ' + reloadpage);

    if ( returndata == '00' ) {
    jQuery("#loginwindow").dialog("close");


    /*
    // send back to index page - replaced by window.location line below
    $.ajax({
    url: 'reloadmain.php',
    success: function(data) {
    alert('Directory created');
    }
    });
    */

    window.location "startpage.php"

    } else {

    if ( 
    error1 =='1') {
    $(
    "[name=memberusername]").css("background-color","pink"); 
    $(
    "#error1 span").text("Invalid userid");    
    } else {
    $(
    "[name=memberusername]").css("background-color","white"); 
    $(
    "#error1 span").text("");    
    }

    if ( 
    error2 =='1') {
    $(
    "[name=memberpassword]").css("background-color","pink"); 
    $(
    "#error2 span").text("Invalid password");    
    }  else {
    $(
    "[name=memberpassword]").css("background-color","white"); 
    $(
    "#error2 span").text("");    


    }    
    // end of else

    }
    });

    return 
    false;
    });

    });
    </
    script
    The hunk of PHP contains a call to a function:

    PHP Code:
     checkpassword(strtoupper($username), strtoupper($password), """");
        
    // Values returned in Array from procedure 
        
    $errorid $checkpass['error7'];    
        
    $errordesc $checkpass['error100'];
        
        
    // check the userid & password 
        
    switch ($errorid)
        {
      case 
    ""// no error
      
    $screenerror1 "0";
      
    $screenerror2 "0";
      break; 
      
      case 
    "CPF2204":   // not a valid userid
      
    $screenerror1 "1";
      break;  
      
      case 
    "CPF22E2":  // not valid password 
      
    $screenerror1 "0";
      
    $screenerror2 "1";
      break; 
      
      default:        
    // handle any other errors
      
    $screenerror1 "1";
      
    $screenerror2 "0";
      break; 
      
        }
        
        echo(
    $screenerror1 .  $screenerror2); 
    The function
    PHP Code:
    //------------------------------------------------------//
    // START:  check password                               //
    //------------------------------------------------------//

    function checkpassword($INusername$INpassword$INerrorid$INerrordesc)
    {

        
    // Make all global variables available here
        
    foreach($GLOBALS as $arraykey=>$arrayvalue
        {
      if(
    $arraykey != 'GLOBALS')
      {
       global $
    $arraykey;
      }
        }
        

          
    $options = array('i5_naming' => DB2_I5_NAMING_ON);
          global 
    $db2conn,$checkpass;
          
    $db2conn xl_db2_connect($options);

          if(!
    $db2conn)
          {
           die(
    'Could not connect to database: ' db2_conn_error()); 
          }

          
    $conn =  xl_i5_connect( );
          
    xl_set_libl("LSA");


        
    /********** Step 1: set up parameter list description ************/
        /* Create an array that specifies the name of      */
        /* the input, the IO being performed, the variable */
        /* type, and the variable name.                    */
        
    $description = array(
          array(
          
    "Name"=>"username",
          
    "IO"=>I5_IN,
          
    "Type"=>I5_TYPE_CHAR,
          
    "Length"=>"10"
         
    ) ,
             array(
          
    "Name"=>"password",
          
    "IO"=>I5_IN,
          
    "Type"=>I5_TYPE_CHAR,
          
    "Length"=>"10"
         
    ) ,
             array(
          
    "Name"=>"errorid",
          
    "IO"=>I5_OUT,
          
    "Type"=>I5_TYPE_CHAR,
          
    "Length"=>"7"
         
    ) ,
             array(
          
    "Name"=>"errordesc",
          
    "IO"=>I5_OUT,
          
    "Type"=>I5_TYPE_CHAR,
          
    "Length"=>"100"
         
    )    
        );


        
    /********** Step 2: prepare the program for calling **************/

        
    $libl xl_get_parameter("LSA");
        
    xl_set_libl($libl);


        
    $pgm i5_program_prepare("JAMIELIB/WEBLOGIN"$description);

        
    // Check if any errors occurred.
        
    if (!$pgm)
        {
         die(
    "i5_program_prepare");
        }

        
    /********** Step 3: assign the parameter values to send **********/
        /* Create an array with the input names and the    */
        /* corresponding values.  You need to supply the   */
        /* actual parm values.                             */
        
    $in_parameters = array(
      
    "username"=>$INusername ,
      
    "password"=>$INpassword ,
      
    "errorid"=>"",
      
    "errordesc"=>""    
        
    );


        
    /********** Step 4: the parameters that will be returned *********/
        /* Create an array with the output parameters      */
        /* returned by the program.                        */
        
    $out_parameters = array(
      
    "username"=>"" ,
      
    "password"=>"" ,
      
    "errorid"=>"errorid" ,
      
    "errordesc"=>"errordesc"    
      
    );


        
    /********** Step 5: Call the program. ***************************/
        
    $ret i5_program_call($pgm$in_parameters$out_parameters);

        
    // Check if any errors occurred.
        
    if (!$ret
        {
        
         
    $errorTab i5_error();
            
    var_dump($errorTab);
            exit;

        
        
    // die("i5_program_call");
        
    }

         
    $checkpass['error7'] = $errorid;
         
    $checkpass['error100'] = $errordesc;
         return 
    $checkpass;
          
       
        
    // Close the program handle (unload from memory).
        
    i5_program_close($pgm);
        
        
       
       }

    //------------------------------------------------------//
    // END:  check password                                 //
    //------------------------------------------------------// 
    This line of code returns the value from the RPG back to the JQUERY
    PHP Code:
    echo($screenerror1 .  $screenerror2); 
    The RPG -- something like this (can't post completed program):
    PHP Code:
          *-------------------------------------------------------------------------------------------  
          * 
    PROGRAM WEBLOGIN                                                                          
          
    PURPOSE Web login validation and return                                                   
          * 
    WRITTEN 10/30/2012                                                                        
          
    AUTHOR  jamie flanary                                                                     
                                                                                                        
          
    PROGRAM DESCRIPTION                                                                         
          
    *   Web login validation and return                                                           
          *                                                                                             
          * 
    INDICATOR USAGE                                                                             
          
    *   03 leave current screen                                                                 
          
    *                                                                                             
          *-------------------------------------------------------------------------------------------  
          *                                                                                             
          * *
    entry plist                                                                                
          
    *                                                                                             
         
    d WEBLOGIN        pr                                                                           
         d  userid                       10    
    const                                                    
         
    d  Password                     10    const                                                    
         
    d  ErrorCode                    07    options(*nopass:*omit)                                   
         
    d  Message                      40    options(*nopass:*omit)                                   
                                                                                                        
         
    d WEBLOGIN        pi                                                                           
         d  userid                       10    
    const                                                    
         
    d  Password                     10    const                                                    
         
    d  ErrorCode                    07    options(*nopass:*omit)                                   
         
    d  Message                      40    options(*nopass:*omit)                                   
                                                                                                        
          * 
    variables                                                                                   
         d CCSID           s             10i 0 inz
    (37)                                                  
         
    d count           s              3  0 inz                                                      
         d PasswordLen     s             10i 0 inz
    (10)                                                  
         
    d ProfileHandle   s             12    inz                                                      
         d WReplaceData    s            100A   Inz
    (*Blanks)                                             
         
    d WMessage        s            100a   Inz(*blanks)                                             
         
    d WMsgFile        s             10A                                                            
         d WMsgLibrary     s             10A                                                            
         d WMsgFileLib     s             20A                                                            
         d WMaxMsgLen      s             10i 0 inz
    (%size(WMessage))                                     
                                                                                                        
         
    dPasswordDS       ds                  Qualified                                                
         d bytesProvided                 10i 0                                      Bytes Provided      
         d bytesAvailable                10i 0                                      Bytes Available     
         d errorID                        7                                         Exception Id        
         d reserved                       1                                         Reserved            
         d ErrorString                  250    Varying                                                  
                                                                                                        
         d ErrorDS         ds                  qualified                                                
         d    bytesProv                  10i 0 inz
    (0)                                                   
         
    d    bytesAvail                 10i 0 inz(0)                                                   
                                                                                                        
         
    d RTVM0100        ds                  qualified                                                
         d    WMsgLen                    10i 0 overlay
    (RTVM0100:9)                                      
         
    d    WMsgData                32767a   overlay(RTVM0100:25)                                     
                                                                                                        
          
    //                                                                                            
          //  external called programs                                                                  
          //                                                                                            
                                                                                                        
         
    d $checkpassword  pr                  ExtPgm('QSYGETPH')                                       
         
    d   UserID                      10a   const                                                    
         
    d   password                    10a   const                                                    
         
    d   ProfileHndl                 12a                                                            
         d   QUSEC                             likeds
    (PasswordDS)                                       
         
    d   PasswordLen                 10i 0 const                                                    
         
    d   CCSID                       10i 0 const                                                    
                                                                                                        
         
    d $GetMessage     pr                  extpgm('QMHRTVM')                                        
         
    d  WRcvVar                   65535A   Options(*VarSize)                                        
         
    d  WRcvVarLen                   10I 0 Const                                                    
         
    d  WFormat                       8A   Const                                                    
         
    d  WMsgID                        7A   Const                                                    
         
    d  WQualMsgF                    20A   Const                                                    
         
    d  WReplData                 65535A   Const Options(*VarSize)                                  
         
    d  WReplDataLen                 10I 0 Const                                                    
         
    d  WDoReplace                   10A   Const                                                    
         
    d  WUseCtrlChars                10A   Const                                                    
         
    d  WErrorCode                65535A   Options(*VarSize)                                        
         
    d  WRetrOpt                     10A   Const Options(*NoPass)                                   
         
    d  WRetrOpt                     10A   Const Options(*NoPass)                                   
         
    d  WCCSID_out                   10I 0 Const Options(*NoPass)                                   
         
    d  WCCSID_in                    10I 0 Const Options(*NoPass)                                   
          *------------------------------------------------------------                                 
          *  
    M A I N    L I N E                                                                         
          
    *------------------------------------------------------------                                 
                                                                                                        
    12345 /free                                                                                         
                                                                                                        
                $checkpassword
    userid :                                                                
                                
    Password :                                                              
                                
    ProfileHandle :                                                         
                                
    PasswordDS :                                                            
                                
    PasswordLen :                                                           
                                
    CCSID);                                                                 
                                                                                                        
                
    errorcode =  PasswordDS.errorID;                                                        
                if 
    errorcode > *blanks;                                                                 
                 
    exsr $getTheMessageDescription;                                                        
                 
    Message Wmessage;                                                                    
                endif;                                                                                  
                                                                                                        
                *
    inlr = *on;                                                                            
                                                                                                        
            
    //--------------------------------------------------------                                  
            // $GetTheMessageDescription - return message description                                   
            //--------------------------------------------------------                                  
                 
    begsr $GetTheMessageDescription;                                                       
                                                                                                        
                   
    WMsgFile 'QCPFMSG';                                                                
                   
    WMsgLibrary 'QSYS';                                                                
                   
    WMsgFileLib WMsgFile WMsgLibrary;                                                
                   
    WReplaceData userid;                                                               
                                                                                                        
                   
    $GetMessage(RTVM0100                                                                 
                                 
    : %sizeRTVM0100 )                                   // return data   
                                 
    'RTVM0100'                                          // return data   
                                 
    errorcode                                           // msg id        
                                 
    WMsgFileLib                                         // msg file,     
                                 
    WReplaceData                                        // msg parameter 
                                 
    : %size(WReplaceData)                                 // msg parameter 
                                 
    '*YES'                                              // substitute MSG
                                 
    '*NO'                                               //  control chars
                                 
    ErrorDS                                                              
                                 
    );                                                                     
                                                                                                        
                   if (
    RTVM0100.WMsgLen > *Zeros);                                                      
                    if (
    RTVM0100.WMsgLen WMaxMsgLen);                                                 
                     
    RTVM0100.WMsgLen WMaxMsgLen;                                                     
                    endif;                                                                              
                    
    // retrieve the message with message data inserted                                  
                    
    WMessage = %subst(RTVM0100.WMsgData RTVM0100.WMsgLen);                        
                   else;                                                                                
                    
    WMessage = *Blanks;                                                                 
                   endif;                                                                               
                                                                                                        
                 
    endsr;                                                                                 
                                                                                                        
          /
    end-free 
    Attached Files
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com
Working...
X