Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>

share|improve this question
1  
any particular reason why you are not using a library like jQuery? –  SeanNieuwoudt Jun 17 '12 at 20:14
    
Thanks Sean. I think I'll try JQuery –  Jeremy Jun 19 '12 at 1:01
    
Hi, the code seems correct. I suggest that you use Chrome and its console to inspect the call and see if your browser is really posting your data. If it is the case you will now that the problem can be on php side. –  Paul Mar 31 at 5:51

1 Answer 1

  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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.