I am developing an app in phonegap, and I am trying to pass formdata from phonegap to a remote server with jquery and json and save it to the mysql database.
I have never used json before, and I am really uncertain of how to recieve and parse this data on the server.
Could anyone help me write a simple php script to handle this?
This is the form and jquery I am using to send data:
<form method="post" id="infoForm">
<input type="text" name="first_name" id="first_name" value="" placeholder="First Name" />
<input type="text" name="last_name" id="last_name" value="" placeholder="Last Name" />
<input type="text" name="email" id="email" value="" placeholder="Email" />
<button type="submit">Submit</button>
</form>
$('#infoForm').submit(function() {
var postTo = 'http://siteurl.com';
$.post(postTo,({first_name: $('[name=first_name]').val(), last_name: $('[name=last_name]').val(), email: $('[name=email]').val()}),
function(data) {
alert(data);
if(data != "") {
// do something
} else {
// couldn't connect
}
},'json');
return false;
});
I have been trying to find a solution for hours without any result.
Thank you!
First of all you can avoid structure like this:
With jQuery’s built in serialize method:
As far as you already specified “json” as a response type “data” variable will contain already unserialized JSON object.
In php script side of things you need to do following:
Of course you need to change $_POST superglobal with your array/object of result, I used it for demo purpose in script line above.
Obviously $_POST variable contains data passed from Ajax request, such variable is available in any scope in PHP script.