Reputation: 2658
Is there a way to tell PHP which form has been submitted?
Form 1
<form id="factory" action="bullets.php" method="post">
<input type="submit" value="Kopen">
</form>
And form 2
<form id="localFactory" action="bullets.php" method="post">
<input type="submit" value="Kopen">
</form>
These forms are on one page.
My javascript code:
var url;
$('form').submit(function (event) {
event.preventDefault();
url = $(this).attr('action');
location.hash = url;
$.ajax ({
url: url,
method: 'POST',
data: $(this).serialize()
}).done(function (html) {
$('#content').html(html);
});
});
If i got an input i get a $_POST variable.
So i need to know which of the above forms has been submitted?
Thanks..
Upvotes: 0
Views: 85
Reputation: 129
If you want an html only solution, you could add a hidden input with the form id:
<form id="factory" action="bullets.php" method="post">
<input type="hidden" value="factory" name="formId"/>
<input type="submit" value="Kopen">
</form>
And then test it with:
if (isset($_POST['formId']) && $_POST['formId'] == 'factory') {
//Do what you want here
}
Upvotes: 0
Reputation: 6946
By namespacing the input fields, you can easily identify which fields are from which form, and by extension which form was submitted.
<form id="factory" action="bullets.php" method="post">
<input type="text" name="form_1[my_input]">
<input type="submit" value="Kopen">
</form>
<form id="localFactory" action="bullets.php" method="post">
<input type="text" name="form_2[my_input]">
<input type="submit" value="Kopen">
</form>
Then it is as simple as:
if (isset($_POST['form_1'])) {
// This post variable is an array of each field.
}
Upvotes: 0
Reputation: 1731
This will work:
var url;
$('form').submit(function (event) {
event.preventDefault();
url = $(this).attr('action');
location.hash = url;
var data = $(this).serialize();
data += "&formId=" + encodeURIComponent($(this).attr('id')); // if you have data in the form.
// do this if you don`t have data in the form:
// data = {formId: $(this).attr('id')};
$.ajax ({
url: url,
method: 'POST',
data: data
}).done(function (html) {
$('#content').html(html);
});
});
You can then get the forms Id from $_POST['formId']
Upvotes: 3
Reputation: 15053
Create a submit button with a name:
<form id="factory" action="bullets.php" method="post">
<button type="submit" value="factory" name="submit">Kopen</button>
</form>
This value is now posted:
if (!empty($_POST['submit']) && $_POST['submit'] == 'factory') {
}
Upvotes: 1