I have an AJAX call (not using JQuery), set up in the following way:
<script type="text/javascript">
var ajax = new XMLHttpRequest();
ajax.open("POST", "testing.php", true);
ajax.onreadystatechange = function(){
if(ajax.readyState == 4 && ajax.status == 200){
var returnVal = ajax.responseText;
if(returnVal == "upload_success"){
$('#display').html("WORKING!");
}else{
$('#display').html("NOPE");
}
}
}
</script>
And I pair it with the following test PHP:
<?php
if(isset($_POST['username'])){
echo "upload_success";
exit();
}
?>
If I send the send the AJAX data in the following way:
ajax.send("username=1");
then everything works perfectly. The inner HTML of the display div is set to the expected "WORKING!"
But if I send the AJAX this way:
var formData = new FormData();
formData.append("username", 1);
ajax.send(formData);
it sets the HTML to "NOPE" - meaning that the post variable $_POST['username'] was not set.
How do I check in my PHP function for this specific AJAX call? I need to be able to differentiate between different AJAX calls.
I will eventually be appending a file to formData, but I was just testing this out first, as I have never used it before.
Thank you in advance.
EDIT 1 - HTML
<?php ... PHP from above goes here ... ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex, nofollow"/>
<title>Title</title>
<link href="../CSS/styles.css" rel="stylesheet" type="text/css" />
<link href="../CSS/UI/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../scripts/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../scripts/UI/jquery-ui.min.js"></script>
<script type="text/javascript"> ... AJAX script from above goes here ... </script>
</head>
<body>
<div id="wrapper">
<form enctype="multipart/form-data" onsubmit="return false;">
<input type="file" name="fileField" id="fileField" />
<br/><br/>
<input class="button" type="submit" value="Upload"/>
</form>
<div id="display"></div>
</div> <!-- End wrapper -->
</body>
</html>
var_dump($_POST);array(1) { ["------WebKitFormBoundarylltcGU2lxnTiY4FC Content-Disposition:_form-data;_name"]=> string(61) ""username" 1 ------WebKitFormBoundarylltcGU2lxnTiY4FC-- " }ajax.send("username=something&file=" + file);