简体   繁体   中英

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

im 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

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

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('&');

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM