Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL encoding a string from JS to Drupal

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?

like image 782
Cris Avatar asked Dec 12 '22 02:12

Cris


1 Answers

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),
like image 51
Vishal Avatar answered Dec 29 '22 03:12

Vishal