简体   繁体   中英

& php post receive problem

my ajax request is

ajaxRequest.send("var1=" + var1.value + "&var2=" + var2.value + "&var3=" + var3.value); 

but the browser sends the request as

var1= hello&var2= world&var3=1

php side receives the data as a post

$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
$variable3 = $_POST['var3'];

only the first variable receives the data. The las two variables do not receive any data.

the correct format of sending the request as i know is

var1= hello&var2= world&var3=1 

But why did the browser appended some useless characters that made the php side unable to recognise what has been sent ?

What happen's when you use urlencode ?

You could put the data string into a variable first then urlencode that veriable and pass the variable inside the ajaxRequest.send

The problem is your ampersands are encoded before sending the request to the browser. This way, the next variable is not recognized. Instead, the first variable contains: hello&var2= world&var3=1 . I don't know which javascript library you're using, but it seems to me either you are using the wrong function arguments (maybe you need to inject an object instead of a string?), or the function wrongly encodes the string.

Take jQuery's ajax method for example, query parameters are passed as an object, like this:

var data = { 'var1' : var1.value, 'var2' : var2.value };
$.ajax('/ajax/', { 'data': data});

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