Update: I dont want to add the hidden field as it may be tampered by user (inspect element)?
Refer to the code below from this question.
// this is the id of the form
$("#idForm").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
In my action.php
how do I know the $_POST
data from #idForm
? I may have different form posted to action.php
. For example:
$("#FormAdd").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully added
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
$("#FormEdit").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully updated
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
$("#FormDelete").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully deleted
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});