AJAX works fine, but $_POST does not have a value.
What I have tried:
$data = json_decode(file_get_contents('php://input'), true);
&$post = json_decode($data);
into storecart.php- Changing the data into
'jCart=' + jData'
- removing datatype (Jaromanda X)
- answer (Umakant Mane)
cart
is an array of objects
Javascript:
$(document).ready(function(){
$("#showcart").click(function(event){
event.preventDefault();
showcart();
url = 'cart.php';
$(location).attr("href",url);
});
});
function showcart(){
var jData = JSON.stringify(cart);
$.ajax({
url:"storecart.php",
type:"post",
data: {jCart : jData},
datatype: "json",
success: function(data){
console.log("SUCCESS")
console.log(jData);
},
error: function(data){
console.log("REDO")
}
});
}
storecart.php:
<?php
if(isset($_POST['jCart'])){
echo "Right";
}else{
echo "Wrong";
}
?>
How do get the $_POST
to accept the json.stringify
?
SOLUTION:
SOLVED:
All i did was add a form that has a hidden value
<form id = "postform" action = "cart.php" method = "post">
<input type = "hidden" id="obj" name="obj" val="">
<input type = "submit" value = "Show Cart" id = "showcart">
</form>
In the Javascript:
$(document).ready(function(){
$("#showcart").click(function(){
var json = JSON.stringify(cart)
$('#obj').val(json);
$('#obj').submit();
});
});
Thank you for everyone that has answered but hope this helps.