1

I've tried everything, I even tried sending other data from my code in JavaScript and nothing works, PHP keeps telling me that $_POST is empty, whenever I try file_get_contents("php://input") it also gives me an empty array, or a null or whatever. I'm trying to send this through JSON, I think that isn't exactly necessary but either way, PHP receives nothing, regardless of what I send. I checked through chrome to see if something was getting sent, and it was the POST request gets logged in Network, and the request payload displays the data I'm trying to send but whenever I open my PHP page in my browser it displays null, Array(), or empty. I thought it might have something to do with UTF-8 but I don't think so. I've been trying this for hours. There has got to be something wrong in here that I'm not seeing.

Here is my javascript code:

window.onload = function findSubmitButton() {
    var button = document.querySelector(".send_info").addEventListener("click", serverInteraction);
}

function serverInteraction() {
  var xmlhttp;
  var inputArray;
  var finalArray = [];
  var JSONArray;
  var userId;
  if (window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      throw new Error("Your Browser is not compatible with XMLHTTP");
      return false;
  }
  inputArray = document.querySelectorAll("input[type=text]")
  for(var i = 0; i < inputArray.length; i++){
      finalArray[i] = inputArray[i].value;
  }
    console.log(finalArray);
    JSONArray = JSON.stringify({finalArray: finalArray}); 
    console.log(JSONArray);
    xmlhttp.open("POST","phpFiles/sendUserInfo.php", true);
    xmlhttp.setRequestHeader("Content-type","application/json;charset=UTF-8");
    xmlhttp.send(JSONArray);
}

This is my PHP for now:

<?php
    $connection = oci_connect('User', 'password', 'localhost/dbXDB');
    var_dump($_POST);
    if(!$connection){
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    else {
        echo "Connection Established";
    }

?>

Ignore the database connection stuff, it's pretty much irrelevant until I can get my data through. I would really appreciate if someone could help me out with this, it cannot be as hard or difficult as it has been, since a lot of other people have done this before. I really can't figure out why it doesn't seem to work.

EDIT: This is what my response log says:

   {"finalArray":["1","2","3","4","5","6"]}Connection Established

I see my JSON right there, I don't know if that means that it worked. The console log still gave me nothing.

Argus
  • 911
  • 3
  • 9
  • 20
  • 2
    Are you opening the phpFiles/sendUserInfo.php page manually to check the var_dump? When you send the POST to this PHP file, you won't see the output with the code you've supplied because the POST is happening asynchronously. You cannot open the PHP page manually after this has happened and expect to see the posted data. Is this what you have tried? – Drakes Mar 30 '15 at 04:18
  • What xmlhttp.responseText is saying???? – Ammar Hayder Khan Mar 30 '15 at 04:58
  • I'm not sure I get what you're saying. If you mean that I'm opening my PHP file by opening in chrome by clicking it or something and opening it with chrome, I'm not doing that. I'm placing it on localhost with its directory name. If that counts as manually opening the file then I guess I'm doing that. I did get the console log for the response, but it says nothing, there's just a white line. I'm new at this, and this isn't taught at college, I've been pretty much learning it as I go. – Argus Mar 30 '15 at 05:20
  • so var_dump(file_get_contents("php://input"));` gives you nothing? – Musa Mar 30 '15 at 06:21

1 Answers1

0

Please console log your xmlhttp.responseText, what is saying? The easier way to use ajax is with jQuery here

Community
  • 1
  • 1
Jakub Kontra
  • 586
  • 1
  • 5
  • 19
  • I understand that JQuery is simpler, but this is my first time with web development and I don't want to get used to the simple method and then find out I don't know anything about JavaScript. – Argus Mar 30 '15 at 05:23
  • Yes, I'm understand you, but. With this simple script you'll do more than you ever now imaginate. If you want, i will explain you by skype or something. It take times. – Jakub Kontra Mar 30 '15 at 05:53