I'm trying to pass an array from JavaScript to PHP using JSON. I created a JSON using JSON.stringify
and I know to decode it in PHP using json_decode
but how can I pass it? To be clear, those variables are in a one php file.
I want to use it like this:
<script>
var arr = getArray();
var myJSON = JSON.stringify(arr);
... passing myJSON to PHP ...
</script>
<?php
$arr = //get myJSON
?>
I tried this:
<script>
var signalff = ff(signal);
request = $.ajax({
type: "POST",
url: "ecg_database.php",
data: {myData:JSON.stringify(signalff)}
});
request.done(function (response, textStatus, jqXHR){
console.log("Hooray, it worked!");
});
request.fail(function (jqXHR, textStatus, errorThrown){
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
</script>
<?php
$obj = $_POST["myData"];
echo $obj;
?>
And in console I have a message "Hooray, it worked!", but it doesn't write anything by echo from $obj.