Reputation: 91
I am using the following code to submit a form with ajax /php.
<form method="post" action="">
<input name="saqhiddenid" class="saqhiddenid" type="hidden" value="' . $pci_saq_id . '" />
<button type="button" name="editsaq" class="editsaq">Edit</button>
</form>
$(function () {
$(".editsaq").click(function(){
$.ajax({
url: 'someurl',
type: "POST",
data: {
saqhiddenid: $('input[name="saqhiddenid"]').val()
},
dataType: "JSON",
success: function (data) {
some code
}
});
});
});
The submit works as it should. I can see in the HTML code that the values of " saqhiddenid " are different, but when i am submitting the form, i always get the results from the highest value.
Any ideas?
Upvotes: 1
Views: 57
Reputation: 842
That is because you don't disable the actual form submit. so you make your ajax call, but the form also gets submitted with the old value. You need:
$(".editsaq").click(function(e){
e.preventDefault();
var form = $(this).closest('form');
var value = form.find('input[name="saqhiddenid"]').val();
$.ajax({
url: 'someurl',
type: "POST",
data: {
saqhiddenid: value
},
dataType: "JSON",
success: function (data) {
some code
}
};
Upvotes: 1
Reputation: 7065
In case of multiple forms with same input name, it will always pick the first input value.
If your form
structure is same as posted try below code:
data: {
saqhiddenid: $(this).prev().val()
}
Upvotes: 1