reju
reju

Reputation: 63

javascript ajax post words which contains & symbol and decode in php

i m trying to send ajax post which contains '&'.

search.php?region=Middle%20East%20&%20Africa&city=&language=

but it return 'Middle East' only in php side

Please help me

Upvotes: 0

Views: 1775

Answers (2)

Gavin Anderegg
Gavin Anderegg

Reputation: 6391

To use the & character in a URL, you must first URL encode it. PHP has a function for doing this called urlencode, which can help. To make the above string work, it should look like:

search.php?region=Middle%20East%20%26%20Africa&city=&language=

Where the & becomes %26. Here's the code I wrote to find this:

<?php

print urlencode('&');

?>

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

If you are using jquery:

$.ajax({
    url: 'search.php',
    type: 'POST',
    data: { region: 'Middle East & Africa', city: '', language: '' },
    success: function(result) {
        // ...        
    } 
});

If not you could manually URL encode the value using the encodeURIComponent function:

var region = encodeURIComponent('Middle East & Africa');
// TODO: send the encoded value

Upvotes: 1

Related Questions