I have a web page that sends form data to php using jQuery. I want to decode the data array and loop through it, displaying each of the four values using php print_r.
Here is the jQuery function to post the data to php:
<script type="text/javascript">
function postData() {
return $.ajax({
var datastring = $("#echo_test").serialize();
$.ajax({
type: "POST",
url: "echo_test.php",
data: {post: datastring},
});
});
}
</script>
Here is the php function echo_test:
<?php
$obj = json_decode($_POST['post']);
print_r($obj['firstname']);
?>
I think json_decode is wrong because the data posted is not json format.
When I click the button that calls the jQuery function, the php page opens in the browser but it's blank, with nothing echoed back. Ideally I would I loop through $obj to display each of the four values on the screen.
Ultimately I will post the data to Postgres on a cloud server. For now, I'm testing it locally to verify the data because I'm new to passing data to php from jQuery.
Thanks for any help with this.