Reputation: 12194
I have a Drupal web app that uses Ajax; the Ajax function, sometimes, needs to pass string as parameters to a Drupal function as
$.ajax({url: "index.php?q=get_value/"+encodeURIComponent(value),
When the value contains a slash it is not recognized from the Drupal function made as
function get_value($value) {
print urldecode($value);
For example, if the passed string is ABC/123 , get_value prints only ABC
How can i solve this problem passing slashes and getting the entire string from PHP/Drupal?
Upvotes: 4
Views: 1540
Reputation: 2393
You should check if the $value parameter to your PHP function has the part after the slash, before URL decoding it.
One way to get around this problem would be to replace the slashes in the string before encoding it, and then replacing those back after decoding it in PHP. Here's an example that replaces the slashes with carriage return characters: (you should use a character which would otherwise not be found in your string)
$.ajax({url: "index.php?q=get_value/"+encodeURIComponent(value.replace("/", "\r")),
And the PHP code to reverse this:
function get_value($value) {
print str_replace("\r", "/", urldecode($value));
You might need to experiment with some other characters (you could also use a character sequence instead of a single character) and see what works. The logic that's stripping out URL-encoded slashes might also strip out other characters.
Upvotes: 1
Reputation: 2181
Use %2f
instead of /
as:
$.ajax({url: "index.php?q=get_value" + "%2f" + encodeURIComponent(value),
[reference], See Section 2.2
Or, a more readable alternative:
$.ajax({url: "index.php?q=" + encodeURIComponent("get_value/"+value),
Upvotes: 7
Reputation: 22412
your problem is that Drupal interprets the slashes as path delimitors so it extracts as $value the first parameter in get_value/val1/val2/val3 ... you can let Drupal split the arguments but you need to recombine them. Following is an example with Drupal7.
In your menu system you have to make sure that drupal will callback your get_value function with all the parameters :
function mymodule_menu {
$items['get_value'] = array(
'page callback' => 'mymodule_get_value',
);
return $items;
}
then recombine the dynamic number of arguments :
function mymodule_get_value() {
$args = func_get_args();
$value = implode('/', $args);
// do what you need with $value here
}
Hope this helps !
Upvotes: 3
Reputation: 8380
It would be helpful to know what your routing system is doing, for a start, what do you see when you debug?
function get_value($value) {
var_dump($value);
print urldecode($value);
Another option that you can try would be to pass the variable with double quotes around it, so they interpreted as string.
$.ajax({url: "index.php?q=get_value/\"" + encodeURIComponent(value) + "\"",
Upvotes: 1
Reputation: 895
Can you give some more details as to where the PHP function is - is it in a Drupal custom module? If it's in a custom module, are you using a menu_hook for the get_value path? Also, which version of Drupal are you using? Another thing is that you can replace encodeURIComponent with Drupal.encodeURIComponent (Drupal 6) or Drupal.encodePath (Drupal 7).
Upvotes: 1
Reputation: 14798
If that is your exact code then you have a syntax error (an extra quote), the first line should be:
$.ajax({url: "index.php?q=get_value/" + encodeURIComponent(value),
Upvotes: 1
Reputation: 2167
Maybe I'm missing something here but can't you just concatenate your query string and pass it into the ajax
function? Maybe you can use parse_url().
Your get_value()
might be an issue. From PHP's urdecode() manual: "The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
Upvotes: 1