Using links through POST, not GET (view mode)

Talk about link fields or link implementations

Moderator: alexandrleonenko

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

Using links through POST, not GET (view mode)

Postby lambro » Dec 21st, '10, 08:47

...i.e., passing parameters not through the url address.

Hi admin, I've been working with ApPHP for quite some times now, and I would like to share some stuff I've been using recently...
Reading http://www.apphp.com/forum/viewtopic.php?f=33&t=1531 and http://www.apphp.com/forum/viewtopic.php?f=33&t=391, I've come upon the need to pass parameters through POST method via links.

Of course if the topic has already been covered somewhere else in the forum, then shame on me :oops: !!!
Still, by combining the two technics depicted above, one can do the following :
USING Datagrid Pro 6.4.1, $dgrid->SetPostBackMethod("POST"); (doesn't matter, I only use view mode here)
#1 create an image link field in the SELECT statement.

Code: Select all

'<img src=\"images/CART.png\" alt=\"Cart\" width=\"35\" height=\"35\">' as btnaj,


#2 I don't even need to uncomment and describe the passing parameters with:

Code: Select all

##  *** set variables that used to get access to the page (like: my_page.php?act=34&id=56 etc.)
    ## +-- Passing parameters & setting up other DataGrids ------------------------+
    // $http_get_vars = array("idoc", "namdoc", "ldoc", "verdoc");
    // $dgrid->SetHttpGetVars($http_get_vars);


#3 In the view mode section ( haven't tried with add/edit/detail mode... yet) I describe my parameters the usual way :

Code: Select all

$vm_columns = array(
   "id_prod"=>array("header"=>"id_doc", "type"=>"label","align"=>"left", "width"=>"15%", "wrap"=>"nowrap",
                                  "text_length"=>"4", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", 
                                  "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"false", "on_js_event"=>""),
   "nom_doc"=>array("header"=>$chaine["name"], "type"=>"label","align"=>"left", "width"=>"20%", "wrap"=>"nowrap", 
                                   "text_length"=>"30", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal",
                                     "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
   "version_doc"=>array("header"=>"version", "type"=>"label","align"=>"left", "width"=>"5%", "wrap"=>"nowrap",
                                      "text_length"=>"30", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal",
                                       "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
   "tag" =>array("header"=>$chaine["prev"], "type"=>"link","align"=>"center", "width"=>"10%", "tooltip"=>"true", 
                               "tooltip_type"=>"floating",
                       "field_key"=>"tag", "field_data"=>"tag",
                       "field_key_1"=>"nom_doc",
                        "field_key_2"=>"abrev",
                        "field_key_3"=>"version_doc",
                "rel"=>"", "title"=>$chaine["prev"], "target"=>"_self","href"=>"javascript:void(0)"),
                          "on_js_event"=>"onclick='window.open(\"php/vignette.php?namdoc={1}&ldoc={2}&verdoc={3}\");'"
      "language"=>array("header"=>$chaine['langue'], "type"=>"label", "align"=>"left", "width"=>"15%", "wrap"=>"nowrap",
                               "text_length"=>"12", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false",
                                "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
      "price"=>array("header"=>$chaine['prix'], "type"=>"label", "align"=>"left", "width"=>"15%", "wrap"=>"nowrap",
                           "text_length"=>"5", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false",
                            "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),

            "btnaj" =>array("header"=>$chaine["addto"], "type"=>"link", "align"=>"center", "width"=>"10%", "tooltip"=>"true", 
                               "tooltip_type"=>"floating",
                      "field_key"=>"btnaj", "field_data"=>"btnaj",
                      "field_key_1"=>"id_prod",
                      "field_key_2"=>"nom_doc",
                      "field_key_3"=>"abrev",
                       "field_key_4"=>"version_doc",
                      "rel"=>"", "title"=>$chaine["addto"], "target"=>"","href"=>"javascript:void(0)",
"on_js_event"=>"onclick = postwith('index.php'{template:'php/select_prod.php',idoc:'{1}',namdoc:'{2}',ldoc:'{3}',verdoc:'{4}'})")   
     );


As you can see, the "tag" column is a link that uses a "url based" passing parameters strategy, whereas the "btnaj" uses a javascript function : it is this one that does the POSTING.

I found it here :http://mentaljetsam.wordpress.com/category/programming/javascript/page/2/, and I am very grateful to M. Peter Finch for that simple but powerfull piece of code :

Code: Select all

// Fonction qui génère un POST en javascript. Remplace un window.location.replace. Très utile.
function postwith (to,p) {
   var myForm = document.createElement("form");
   myForm.method="post" ;
   myForm.action = to ;
      for (var k in p) {
         var myInput = document.createElement("input") ;
         myInput.setAttribute("name", k) ;
         myInput.setAttribute("value", p[k]);
         myForm.appendChild(myInput) ;
      }
   document.body.appendChild(myForm) ;
   myForm.submit() ;
   document.body.removeChild(myForm) ;
}

This will do the trick. For this example, the called page url will end up in 'index.php' and a print_r($_POST); report will display, among other parameters if needed :

Code: Select all

  [template] => php/select_prod.php
    [idoc] => 2
    [namdoc] => A70UG006
    [ldoc] => FR
    [verdoc] => 0796


This will help me a lot, so if you think it may be of use to anyone, or if corrections are needed, please share and tell !! @+

Return to “Field type: "link"”