Having troubles passing the ajax to $_POST
, it is always returning false.
What I have tried so far:
- Using contentType:
'application/json; charset=UTF-8'
, - Creating a function for ajax and adding to jQuery onclick
- Removed dataType in Javascript,
console.log: Object {readyState: 0, responseText: "", status: 0, statusText: "error"}
- With dataType in Javascript,
Object {readyState: 4, responseText: "↵ ↵Failed to hold<br>", status: 200, statusText: "OK"}
Javascript:
$(document).ready(function(){
$("#showcart").click(function(event){
event.preventDefault();
$.ajax({
data: {'jCart':JSON.stringify(cart)},
type: 'POST',
dataType: 'json',
url: 'storecart.php',
contentType:'application/json; charset=UTF-8',
success: function(data){
console.log("Success")
},
error: errorFunction
});
});
});
function errorFunction(){
console.log("Error");
}
Storecart.php
<?php
if(isset($_POST['jCart'])){
$decode = json_decode($_POST['jCart']);
$_SESSION['receive'] = $decode;
$product = $_SESSION['receive'];
}
else{
echo "Failed to hold<br>";
}
?>
Cart.php
<?php
session_store();
include(Storecart.php);
?>
On the console, it will say "Error"
.
On the cart.PHP, it will say "Failed to hold"
.
What I know is that ajax is not running properly, I don't know how to fix it.
ALMOST SOLUTION for ajax, but it can't post for some reason
Don't know why but it worked for me.
Create a function:
function showcart(){
var jData = JSON.stringify(cart);
$.ajax({
url:"storecart.php",
type:"post",
data: 'datastring=' + jData,
datatype: "json",
success: function(data){
console.log("SUCCESS")
console.log(jData);
},
error: function(data){
console.log("REDO")
}
});
}
Add it to the javascript:
$(document).ready(function(){
$("#showcart").click(function(event){
event.preventDefault();
showcart();
});
});