views:

247

answers:

3

I have a link and on clicking it, a function is called. In that function, I set the href attribute to pass an id variable. If I alert the href value, I see the correct url along with the Id. But in the corresponding function in the controller, If I do an $_GET of that variable Id, I do not get the value. What is the error?

This is my link:

<a href="http://localhost/FormBuilder/reports/export?height=220&amp;width=350" id="export_entries" class="thickbox button" title= "Export" >Export</a>

And the corresponding on click function:

$("#export_entries").click(function() {
 $(this).attr("href",$(this).attr("href")+"&id="+formid);
 alert($(this).attr("href"));
});

In this alert box,If I click the second link, I get the value as

 http://localhost/FormBuilder/reports/export?height=220&amp;width=350&amp;id=2

But In my export function in the controller I do not get the value. The variable formid is empty.

function export()
   {

        $formid=$_GET['id'];
        echo " formid: ".$formid;
        $this->set('formid',$formid);
   }
+1  A: 

What is probably happening is that you change the href attribute, but the browser redirects to the old unchanged url, since you are handling the click of the anchor element.

You could build the url string, appending the parameter and redirect the user to it directly, using location.href:

$("#export_entries").click(function(e) {
  location.href = $(this).attr("href")+"&id="+formid;
  e.preventDefault();
});
CMS
This works correct, but I want the thickbox plugin to be loaded. Snd if I use location.href, the export view is opened in a new web page and not as a thickbox url. Could u suggest anything to load the thickbox plugin as well as redirect to that url?
Angeline Aarthi
A: 

In CakePHP, parameters work like this:

http://localhost/FormBuilder/reports/export/height:220/width:350/id:2

instead of

http://localhost/FormBuilder/reports/export?height=220&amp;width=350&amp;id=2

You can retrieve the parameters in your controller with

$formid = $this->params['id'];

They are called named parameters. Further reading:

http://book.cakephp.org/view/541/Named-parameters

http://bakery.cakephp.org/articles/view/passing-named-parameters

jimyi
No,in this case only $_GET works !!
Angeline Aarthi
A: 

I found a solution to my problem. Even if I change the href attribute in the JQuery function,the browser is redirected to the old url as said by CMS. But if I give location.href, I get the functionality working, but the thickbox isn't loaded. The thickbox gets loaded only if I set the new url in the href attribute of the link as I had already done, i.e.,

$(this).attr("href","http://localhost/FormBuilder/reports/export?height=220&amp;width=350&amp;id="+formid);

So to change the url dynamically, I went for changing the thickbox.js file as mentioned in this site where we need not mention the href attribute in the link itself. We could set the href attribute after clicking the link,in the click function of the link and add the line

tb_open_new($(this).attr("href"));

after setting the href attribute to open this changed url in the thickbox window..

Angeline Aarthi