I am hitting roadblock after roadblock in my quest to get this JSON usable by PHP, so I'm wondering if someone can help me out here.
I have JSON being stored in the variable divisionsJSON:
var divisionsJSON = JSON.stringify(divisions);
Then I try to use .ajax to post with the following:
$.ajax({
url: "divisions.php",
type: "post",
data: divisionsJSON,
success: function(){
alert("success");
$("#result").html('submitted successfully');
},
error:function(){
alert("failure");
$("#result").html('there is error while submit');
}
});
(I copied this from another question on SO, but have nothing in my html with the id="result" - I think I can delete that part [deletion confirmed])
Then, my divisions.php page contains the following:
<?php
$url = "divisions.php";
$json = file_get_contents($url);
$json = utf8_encode($json);
$elements = json_decode($json);
var_dump($elements);
?>
My inexperience with PHP/ajax combined with my frustration with getting this to work has made me try a bunch of different things. So far I've either gotten NULL or nothing when I load divisions.php. My gut tells me that it's a problem with the ajax, but I'm so inexperienced with PHP that I can't say with confidence that my PHP is correct enough to where I should be getting something back. I've tried var_dump, print_r, echo, absolutely nothing is showing up on divisions.php related to the actual PHP/JSON. Any and all help would be greatly appreciated!
Response with updated code:
I am getting NULL with the following php without the utf8 line, and string(0) "" when I added in the utf8 line:
<?php
$json = json_decode(file_get_contents('php://input'));
$elements = utf8_encode($json);
var_dump($elements);
?>
Any ideas?
Edited for full php page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>untitled</title>
<link rel="stylesheet" href="styles/reset.css" />
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript" src="application.js"></script>
<script type="text/javascript" src="divisions.js"></script>
<?php
print_r($_POST);
?>
</body>
</html>
I've tried multiple lines for the php, print_r($_POST);, json_decode($_POST);, etc. but I'm changing so many things based on what I'm seeing on other SO's and this page that I'm really in a daze.
EDIT: Would this code, which I intended to make an array, not produce JSON? Because it doesn't seem like I'm working with JSON atm.
var divisions = [];
for (var i = 0; i < arr.length; i++) {
if(arr[i].value != "N/A"){
var obj = { "login": arr[i].login,
"value": "http://www.value.net/index/" + arr[i].value};
divisions.push(obj);}
json_decode(file_get_contents('php://input'));$json = file_get_contents($url);is wrong.