when you have an existing form, that should now work with jquery - ajax/post now you could:
- hang onto the submit - event of your form
- prevent default functionality of submit
do your own stuff
$(function() {
//hang on event of form with id=myform
$("#myform").submit(function(e) {
//prevent Default functionality
e.preventDefault();
//get the action-url of the form
var actionurl = e.currentTarget.action;
//do your own request an handle the results
$.ajax({
url: actionurl,
type: 'post',
dataType: 'application/json',
data: $("#myform").serialize(),
success: function(data) {
... do something with the data...
}
});
});
});
Please note that, in order for the serialize()
function to work in the example above, all form elements need to have their name
attribute defined.
Example of the form:
@PtF - the data is submitted using POST in this sample, so this means you can access your data via
$_POST['dataproperty1']
, where dataproperty1 is a "variable-name" in your json.
here sample syntax if you use CodeIgniter:
$pdata = $this->input->post();
$prop1 = $pdata['prop1'];
$prop1 = $pdata['prop2'];