0

Any body has any idea how to encode a value which has comma(,) or any other special character in javascript and then decode the value in a different php page?

javacript code for send values through url

function search(){
    var city_val = $('.city-selection').val();
    var venue_val = $('.venue-selection').val()
    var function_val = $('.function-selection').val();
    var loadpage = "/data/scripts/search_decor.php?city="+city_val+"&venue="+venue_val+"&function="+function_val;
    $('#main_content').load(loadpage, function() {});
}

php code for retreiving values from url

$city = $_GET['city'];
$venue = $_GET['venue'];
$function = $_GET['function'];
4
  • There are a lot of ways. You should enter your query into Google (or similar) to find the answer. Commented Dec 26, 2015 at 18:42
  • @Terminus A lot? Then tell me more than one method how to encode on the JavaScript side (native, without other libraries). Commented Dec 26, 2015 at 18:48
  • @DmitriPavlutin there are many ways to solve the problem he's having. Just cause he asked for a particular solution doesn't necessarily mean it's what would serve him best. (what I posted as a comment on your answer undoubtedly ends up encoding the parameters for you) or you could switch to using POST... fine, "a lot" might have been an exaggeration ;) Still, not a question that couldn't be solved by reading the docs on the function he is already using Commented Dec 26, 2015 at 18:53
  • 1
    @Terminus OK. Happy New Year! Commented Dec 26, 2015 at 19:04

2 Answers 2

1

On the javascript side you need to use encodeURIComponent().
For example:

var city_val = encodeURIComponent($('.city-selection').val());

On the php side you should use urldecode(). For example:

$city = urldecode($_GET['city']);

Check the links for encodeURIComponent() and urldecode().

Sign up to request clarification or add additional context in comments.

4 Comments

Or just use the data parameter of load and let jquery deal with it?
@Terminus Like I mentioned, let's provide native solutions. Someone else reading the question probably will not use jquery.
If the OP is already using jQuery, might as well use jQuery. If you're going to say don't use jQuery, should you replace all the code he has already using it?
@Terminus I suggest to provide generalized solutions, if it wasn't mentioned that jquery is an option. The code could be just an example.
0

Use encodeURIComponent(string) in JS

and string urldecode ( string $str ) in PHP


function search(){
    var city_val = $('.city-selection').val();
    var venue_val = $('.venue-selection').val()
    var function_val = $('.function-selection').val();
    var loadpage = "/data/scripts/search_decor.php?city="+encodeURIComponent(city_val)+"&venue="+encodeURIComponent(venue_val)+"&function="+encodeURIComponent(function_val);
    $('#main_content').load(loadpage, function() {});
}
$city = urldecode($_GET['city']);
$venue = urldecode($_GET['venue']);
$function = urldecode($_GET['function']);

4 Comments

It is not correct to use encodeURI in this case. encodeURIComponent() is necessary.
thanks, will try this solutions and let you know if it works for me.
Ok let me know if you need more help
You are wellcome! (btw did you accept the wrong answer?^^)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.