Question

I have a ul of dynamic buttons in my view that appears like the following:

<ul id="dashboard_list">
  <li id="id_100" class="btn btn-primary">
    <a id="id_100" href="/plugin_name/controller_name/action_name/100">Default View</a>
  </li>
  <li id="id_200" class="btn btn-primary">
    <a id="id_200" href="/plugin_name/controller_name/action_name/200">Second View</a>
  </li>
  <li id="id_300" class="btn btn-primary">
     <a id="id_300" href="/plugin_name/controller_name/action_name/300">Third View</a>
  </li>
</ul>

The above links are created using the JSHelper as follows:

echo $this->Html->link($view->name, '/plugin_name/controller_name/action_name/'. $view->id, array('class' => 'ajax-link', 'id'=> $view->id));

I'm using the script below that I found while researching:

// onClick function
function onClick(){
    $('#view_container').load($(this).attr('href'), onSuccess); 
    return false;
}

// activate ajax links to call the onClick function
$('.ajax-link').live('click', onClick);

// onSuccess-callback function
function onSuccess(){}

Now, in my controller / action im doing a simple check for data as follows:

function actionName() {
    if ($this->data != null) {
        die('We has data!');
    }
    else
    {
        die('We has no data.');
    }
}

My #view_container element updates properly with "We has no data" on every click. So, I'm obviously not communicating the link's view id number (data) between the view and the controller.

Can anyone offer some direction on how to implement this functionality in CakePHP 1.3 to access the selected id (variable) in the controller? I mostly seem to find form submission examples (or just dead links), and I unfortunately don't have the option to upgrade cakePHP.

FYI: The proper helpers, scripts and the js->writeBuffer are being included.

Thank you for any responses in advance!

Was it helpful?

Solution

Rewrite your function as follows:

function actionName($id) {
    debug($id);
    if ($this->data != null) {
        die('We has data!');
    }
    else
    {
        die('We has no data.');
    }
}

If you need to do more than one variable in the URL ex:

href="/plugin_name/controller_name/action_name/300/yellow/bannana"

Then your function would look like:

function actionName($id,$color,$fruit) {
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top