简体   繁体   中英

How to send multiple data with ajax to PHP?

How to send multiple data as URL parameter to server side php script using only Javascript/Ajax.

I don't need to use Jquer.y

I am tying this way:

xhttp.open("GET", 'spec_crawler.php?value='+postValue+'&tablename='+tablename+'&id='+postProdID+'\'', true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send();

On server side I am getting only value:

$html_snippet =$_GET['value'];

Others are empty. but from client side I am sending the proper value.

Am I missing something fundamental?

Content type " application/x-www-form-urlencoded " is usually used for POST requests.
Encode each param value with encodeURIComponent function:

var params = 'value=' + encodeURIComponent(postValue) +'&tablename=' + encodeURIComponent(tablename) +'&id='+ encodeURIComponent(postProdID);
xhttp.open("GET", 'spec_crawler.php?' + params, true);          
xhttp.send();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

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