简体   繁体   中英

POST large array from Javascript to PHP with XMLHttpRequest

I am trying to send a large array from Javascript to mySQL through PHP. I am experimenting with POST but the following doesn't work. The 'alert' in http.onreadystatechange pops open and appears to contain just the code below. Code seems pretty straightforward. Any help is appreciated.

<?php 

if (isset($_POST['params'])) {
echo 'YES isset';
$phpobj = ($_POST['params']);
echo $phpobj;

}else{
echo 'No isset';
}

?>


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
http=new XMLHttpRequest();
}else{// code for IE6, IE5
http=new ActiveXObject("Microsoft.XMLHTTP");
}

var url = "PostArray.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
}
}
http.send(params);

</script>

  1. there's no variable called params in the data you'd like to pass, only lorem and name
  2. you don't even post that, because you haven't passed the variable params (in javascript) to http object

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