tags:

views:

200

answers:

4

I am trying to fetch a variable address from my current URL using JRequest::getVar('address') method. But if the address value has a (#) character, the part after the # character is not retrieved.

I understand that URI is a combination of query + fragment and the part after a hash symbol is treated as a fragment. I have tried to use urlencode method but it still doesn't solve the problem.

Can anyone please tell me how to solve the issue?

+1  A: 

I guess you will have to replace the hash-symbol on your own. For example:

str_replace($the_url, '#', '-');

I don't know, where exactly you have to do that, because I don't know how the Joomla!-Framework handles links and urls. But I am sure, that someone else can help here any further...

faileN
It doesn't seem to be a good solution. The values are entered by the user and to show him the same value, I will have to first replace the '#' symbol with another symbol and then again replace the other symbol with # for each field using str_replace('-', '#' JRequest::getVar('parameter')).I know this would work but is there any better method?
+1  A: 

What is the problem with using urlencode? It should replace # with %23 and all should be well. You can try JRequest::getVar(str_replace('#', '%23', 'address')) which should do the trick. Can you post an example URL that doesn't get properly urlencoded?

Michael Mior
'http://test.com/index.php?ADDRESS=101 Street #6 City'The part after # is treated as a fragment (even after urlencoding)
I just urlencoded that exact URL and it is properly escape for me. What happens when you execute this code in your version of PHP?`<?php echo(urlencode('test.com/index.php?ADDRESS=101 Street #6 City')); ?>`
Michael Mior
(I get `test.com%2Findex.php%3FADDRESS%3D101+Street+%236+City`)
Michael Mior
You get a properly encoded url. But when you try to retrieve a 'get' parameter as JRequest::getVar('ADDRESS'), the part after # is truncated because I guess, # is a fragment identifier.Any thoughts?
Ah, ok. If this is a URL you're generating for your application, then you probably only want to `urlencode` the part after the `?`. Try `'http://test.com/index.php?'.urlencode('ADDRESS=101 Street #6 City')`.
Michael Mior
Just a note that I downloaded Joomla and poked through the code of `JRequest`. It looks like all it's doing is pulling the value from `$_REQUEST`. I tried dumping `$_REQUEST` in a PHP script which has the `urlencode`d parameter above and it seems to pick it up correctly.
Michael Mior
A: 

The part after # is never sent to Apache/PHP, and can therefore not be retrieved by a PHP script. What you need to do, is to url encode the ADDRESS parameter of the URL.

test.com/index.php?ADDRESS=<?= urlencode('101 Street #6 City') ?>

That code will generate the following url

test.com/index.php?ADDRESS=101+Street+%236+City

Now on this URL, you can retrieve address with JRequest::getVar('ADDRESS')

Znarkus
Even after urlencoding the address, I cannot retrieve the address with JRequest::getVar('ADDRESS')Here lies the problem. %23 also does not serve the purpose.
What do you get when you `var_dump($_GET);die;`?
Znarkus
Maybe Joomla! is translating this back into `#` internally. So that you don't get the intended result...
faileN
A: 

Check this Joomla doc out. You can retrieve what Joomla call the 'fragment' by doing:

$uri = 'http://fredbloggs:[email protected]:8080/path/to/Joomla/index.php?task=view&amp;id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'Fragment is ' . $u->getFragment();
Martin