I am running into a brick wall when passing object data from jquery to PHP. I'm still trying to get my head around OOP.
Code follows:
<script type ="text/javascript">
$(function() {
$(".button").click(function() {
//alert("click");
var jsonvar1 = {"skillz": {
"web":[
{"name": "html",
"years": "5"
},
{"name": "css",
"years": "3"
}],
"database":[
{"name": "sql",
"years": "7"
}]
}};
$.ajax({
url: "test2.php",
type: 'POST',
data: 'regemail=' + jsonvar1,
success: function(result) {
alert(result);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
});
});
</script>
</head>
<body>
<input type="button" id="regSubmit" class="button" value="Register!" /></br></span>
<script>
$('input[type=button]').attr('disabled', false);
//alert('test');
</script>
The above code does 3 things. Catches a button click, and then: eclares a variable (jsonvar1), and performs an ajax request on that variable to a PHP backend.
Now the code in the PHP backend:
if (filter_has_var(INPUT_POST, "regemail")) {
$data = $_REQUEST["regemail"];
//echo "<br>I got some data!</br>";
//print_r($data);
//echo $data->skillz;
//echo $data;
var_dump($data);
} else {
echo "No Data.";
}
(safely ignore all the echoes and dumps in the above PHP. That would be me flailing about trying to use the data in some fashion)
Question: How can I pull data from the object in PHP into variables or an array, or if you prefer, how can I work directly with the values in that object? (assuming it is an object, and that I'm not making some other unrelated mistake)
I'm wondering if I've neglected to tell my request that it is JSON... http://api.jquery.com/jQuery.getJSON/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Edit:
I've attempted to add echo json_decode($data); to my PHP code, but it returns a blank dataset.
I also attempted to put dataType: 'json', in my ajax query.
Still not having any luck it seems.