Reading The $_post

  • $_post What?

    $_post contains the broken down uri of the page. Jamroom receives the url, processes anything in HTTP $_POST or $_GET, sorts it out and adds some useful stuff.

    This page gathers together a few examples of $_post as used in smarty templates and php, along with debug output, which hopefully makes it all a bit clearer.

    As with $_user and $_conf you can access $_post in the templates as a smarty variable: {$_post} which is an array. Similarly you can access $_post, $_user and $_conf anywhere in the php as well - they are global variables.

    In a smarty template, print each of the array values like this: {$_post.module_url}, {$_post._2}
    In the php they are usually written as $_post['module_url']
  • What's in the $_post?

    Lets take a look at $_post in the {debug} window when arriving at Admin’s profile index page:
    Quote: {$_post} Array (4)
    _uri => "/admin"
    module_url => "admin"
    module => ""
    _profile_id => "1"

    Note that on a profile, {$_post.module_url} will be the same as {$profile_url}. Jamroom conveniently adds the _profile_id to the array.
  • An Item Detail Page

    On a song detail page {$_post.option} will be a module url:
    Quote: {$_post} Array (7)
    _uri => "/admin/audio/28/testsong"
    module_url => "admin"
    module => ""
    option => "audio"
    _1 => "28"
    _2 => "testsong"
    _profile_id => "1"

    $_post._1 and $_post._2 are unnamed parts of the url, there could be many in a long uri.
    Alternatively use key=value in the url and access value using {$_post.key}, see next.
  • A module view template

    For a module view, there is no {$_post._profile_id}, we have the convenience of {$_post.module} instead
    Quote: {$_post} Array (4)
    _uri => "/mymoduleurl/myview/clothing/thing=special/promo_id=786"
    module_url => "mymoduleurl"
    module => "myModule"
    option => "myview"
    _1 => "clothing"
    thing => "special"
    promo_id => "786"
  • Writing Links In Templates

    So to create a link in a page linking to itself,
    <a href="{$jamroom_url}/{$_post.module_url}/{$_post.option}/{$_post._1}/thing={$_post.thing}/promo_id={$_post.promo_id}">link</a> 
  • or more simply
    <a href="{$jamroom_url}/{$_post._uri} ">link</a> 
  • PHP

    $_post is immediately available in all php module view functions and most other functions as well. In any function where $_post is not immediately available, just global $_post; and it will be made available. So the values in $_post can be reached anywhere in Jamroom. $_post['module_url'], $_post['_2']

    An example from jrBlog/profile.php with debug in the comments:
    //------------------------------
    
    // profile_default
    
    //------------------------------
    
    function profile_view_jrBlog_default($_profile,$_post,$_user,$_conf)
    {
    // fdebug($_post);
    
    
    // (2014-02-13T20:53:35+00:00 0.50365100)-(mem: 10485760)-(pid: 35317)-(uri: /admin/blog/category/testing)
    
    // Array
    
    // (
    
    //     [_uri] => /admin/blog/category/testing
    
    //     [module_url] => admin
    
    //     [module] => jrBlog
    
    //     [option] => category
    
    //     [_1] => category
    
    //     [_2] => testing
    
    //     [_profile_id] => 1
    
    // )
    
    
        if (!isset($_post['_1']) || strlen($_post['_1']) === 0) {
            return false;
        }
        switch ($_post['_1']) {
            // list all categories OR blog posts in a category
  • A module view is very similar. $_post becomes more complicated when forms are involved as all of the form values are sent to the form save function and validated using $_post.

    Here is a function which receives $_post when a simple form is sent, and validates it before sending an invite. Note that $_post['__ajax’] = 1 tells us that this is an ajaxed form.
    //------------------------------
    
    // invite send
    
    //------------------------------
    
    function view_ujTogether_invite_send($_post, $_user, $_conf)
    {
    //fdebug($_post);
    
    // (2013-12-02T00:32:26+00:00 0.53703800)-(mem: 7864320)-(pid: 6061)-(uri: /together/invite_send/__ajax=1)
    
    // Array
    
    // (
    
    //     [_uri] => /together/invite_send/__ajax=1
    
    //     [jr_html_form_token] => f0833dd3179b19bf4950ed8906812c4a
    
    //     [jr_html_form_profile_id] => 1
    
    //     [invite_id] => 2
    
    //     [note_subject] => here you go
    
    //     [module_url] => together
    
    //     [module] => ujTogether
    
    //     [option] => invite_send
    
    //     [__ajax] => 1
    
    // )
    
    
        // Must be logged in
    
        jrUser_session_require_login();
        jrUser_check_quota_access('ujTogether');
    
       // need to cache this before validating
    
        $xid = $_post['invite_id'];
    
        // validate the form
    
        jrCore_form_validate($_post);

Tags