Differences

This shows you the differences between two versions of the page.

Link to this comparison view

computers:php_field_forwarder [2015/03/24 05:42] (current)
Line 1: Line 1:
 +====== PHP Field Forwarder ======
 +This function is great for when you have a multi-page form such as an application. This function forwards fields on one page to hidden fields on the next page, and so on. Very simple to call. The beauty of it is, it also will handle arrays with multiple dimensions. If you have a field with the same name on the next form, it's a good rule to call field_forwarder() at the BEGINNING of your form so that when that form is passed, the field (with the same name) will overwrite the previous form. It's also good to put field_forwarder() at the top so the final page will have all the fields in the order of the application. This is a recursive function, i.e. it re-calls itself when it encounters form elements that are arrays such as: Finally, if you wish to print out PHP code of the array instead of hidden fields, add this line right before you call field_forwarder():​ $FFoutputType = '​print';​ and it will print out, in text form, the values of the fields as php variables -- this is great for storing php code in a database.
  
 +====== Code 1 ======
 +Reference: [[http://​www.zend.com/​code/​codex.php?​id=1092&​single=1]]
 +
 +==== Usage Example ====
 +<code php> ​
 +code goes here
 +
 +</​code>​
 +
 +
 +==== Server Side Code  ====
 +
 +<code php>
 +<?php
 +function field_forwarder(){
 +    global $_POST, $rEM979, $FFoutputType;​
 +    //get the arguments passed
 +    $argList = func_get_args();​
 +
 +    //globalize any other set of instructions
 +    if(count($argList)){
 +        eval('​global $' . $argList[count($argList)-1] . ';'​);​
 +    }
 +    //set the default set of values to convert
 +        if(count($argList)==0){
 +            //if the function is initially passed without parameter we're looking in $_POST
 +            $argList[0] = '​_POST';​
 +            $startValue = $_POST; ​
 +            if(sizeof($startValue)==0){return false;}
 +        }elseif(count($argList)==1){
 +            eval( '​$rEM979["'​ . $argList[0] . '"​] = $' . $argList[0] . ';'​);​
 +            $argList[0] = '​rEM979';​
 +            $startValue = $rEM979;
 +        }elseif(count($argList)==2){
 +            eval( '​$startValue = $' . $argList[1] . '​["'​ . $argList[0] . '"​];'​);​
 +        }else{
 +            for($e=count($argList)-2;​$e>​=0;​$e--){
 +                $intersperse .= '​["'​ . $argList[$e] . '"​]';​
 +            }
 +            eval( '​$startValue = $' . $argList[count($argList)-1] ​ . $intersperse . ';'​);​
 +        }
 +
 +        foreach($startValue as $n => $v){
 +            if(is_array($v)){
 +                //call the function again
 +                $shiftArguments = '';​
 +                for($w=0;​$w<​=count($argList)-1;​$w++){
 +                    $shiftArguments .= '"'​ . $argList[$w] . '",​ ';
 +                }
 +                $shiftArguments = substr($shiftArguments,​ 0, strlen($shiftArguments)-2);​
 +                ​
 +                eval('​$fieldForwarder .= field_forwarder("'​ . $n . '"'​ . substr(',',​0,​strlen($shiftArguments)) ​
 +                                      . ' ' . $shiftArguments . '​);'​);​
 +                            ​
 +            }else{
 +                //we have an root value finally
 +                if(count($argList)==1){
 +                    //actual output
 +                    flush();
 +                    if($FFoutputType == '​print'​){
 +                        $fieldForwarder .= "$$n = '​$v';​n";​
 +                    }else{
 +                        $fieldForwarder .= "<​input type="​hidden"​ name="​$n"​ value=""​ . htmlentities(stripslashes($v)) . "">​n";​
 +                    }
 +                }elseif(count($argList)>​1){
 +                    $indexString = '';​
 +                    for($g=count($argList)-3;​$g>​=0;​$g--){
 +                        $indexString .= '​['​ . ((!is_numeric($argList[$g]) and $FFoutputType=='​print'​)?"'":''​) . 
 +                        $argList[$g] . ((!is_numeric($argList[$g]) and $FFoutputType=='​print'​)?"'":''​) . '​]';​
 +                    }
 +                    $indexString .= '​['​ . ((!is_numeric($n) and $FFoutputType=='​print'​)?"'":''​) . 
 +                    $n . ((!is_numeric($n) and $FFoutputType=='​print'​)?"'":''​) . '​]';​
 +                    //actual output
 +                    flush();
 +                    if($FFoutputType == '​print'​){
 +                        $fieldForwarder .= "​${$argList[count($argList)-2]}$indexString = '​$v';​n";​
 +                    }else{
 +                        $fieldForwarder .= "<​input type="​hidden"​ name="​{$argList[count($argList)-2]}$indexString"​ value="" ​
 +                                        . htmlentities(stripslashes($v)) . "">​n";​
 +                    }
 +                }
 +            }       
 +        }
 +return $fieldForwarder;​
 +}//end field_forwarder()
 +?> 
 +
 +</​code>​
 +
 +==== Client Side Code ====
 +Client side code is inherently insecure and error prone. ​ It should be used to reduce load on server, and input verified ​ by server. ​ Please include server verification code also in this section.
 +
 +Description,​ change title as needed
 +
 +<code javascript>​
 +code goes here
 +
 +</​code>​
 +
 +
 +====== Candidate Code Suggestions ======
 +Add your suggestions and links here.