0

I want to pass a certain url with anchor to cakephp as parameter. my ajax is as follows:

var next_url = 'www.domain.com/search#!query=blah%20secondBlah&times=5';
$.ajax({
    url : '/users/login/?next='+next_url,
    success: function(res) {
        // do something
    }
});     

In my controller, I run

debug($this->request->query['next']);

and it gets me only www.domain.com/search without the anchor part.

What to do?

CakePHP 2.3

mgPePe
  • 5,677
  • 12
  • 52
  • 85
  • what do you have in your controller? – Fury May 15 '14 at 14:11
  • Why do you want to use such an anchor? – Holt May 15 '14 at 14:11
  • @IsaacRajaei what do you mean? – mgPePe May 15 '14 at 14:13
  • try @Brain Glaz answer first and read my comment below it. You will be able to get the next. try that if it doesn't work let me know. – Fury May 15 '14 at 14:16
  • possible duplicate of [Can PHP read the hash portion of the URL?](http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url). What you're trying to do is fundamentally not possible. – AD7six May 15 '14 at 15:30

1 Answers1

1

I would send next_url in the data option of $.ajax so it is properly URL encoded. Try this:

$.ajax({
    url : '/users/login/',
    data: {next: next_url},
    success: function(res) {
        // do something
    }
});  
Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
  • debug($this->request->query['next']); should be debug($this->request->data['next']); – Fury May 15 '14 at 14:14
  • @IsaacRajaei I'm not sure.. does using `data` automatically turn it into a POST request? If it remains GET, I believe jQuery will just append it as a query string. – Brian Glaz May 15 '14 at 14:15
  • `$this->request->data['next']` returns `Undefined index: next`. – mgPePe May 15 '14 at 14:19
  • @mgPePe I believe Isaac was incorrect, stick with your original `debug($this->request->query['next']);` – Brian Glaz May 15 '14 at 14:21
  • 1
    Passing as data: {next: next_url} works indeed, but I still have the same problem on the non-ajax version, which I also need working. `http://www.domain.dev/login?next=http://www.domain.dev/search#!search_term=blah%20&times=5` – mgPePe May 15 '14 at 14:25
  • 1
    you can use JS to encode the URL, and then it should work. Try `next_url = encodeURIComponent('www.domain.com/search#!query=blah%20secondBlah&times=5');` – Brian Glaz May 15 '14 at 14:27
  • That works half-way because my `&amps` get screwed when I debug `$this->request->query['next']`: `www.domain.com/search#!query=blah%20secondBlah&times=5`. It seems that CakePHP somehow autodecodes it, but through a different mechanism than Javascript's `encodeURIComponent` – mgPePe May 15 '14 at 14:33
  • after you retrieve it in cake, try using `html_entity_decode()` on it to get the `&` back. – Brian Glaz May 15 '14 at 14:39
  • Unfortunately this also converts the %20 and I am left with ` ` in the url :( – mgPePe May 15 '14 at 14:59