Reputation: 7128
I'm trying to save data by ajax the issue is that i'm getting same id over and over.
how my data append on blade
if(value1['type'] == 'textfield'){
var my_row = $('<div class="row mt-20 ccin">');
var my_html = '{{ Form::open() }}<input name="product_id" id="product_id" type="hidden" value="{{$product->id}}"><input name="specification_id" id="specification_id" type="hidden" value="'+vvvid+'"><div class="col-md-4">'+value1.title+'</div>';
my_html += '<div class="col-md-6"><input id="text_dec" name="text_dec[]" placeholder="text field" class="text_dec form-control"></div>';
my_html += '<div class="col-md-2"><button type="button" id="custmodalsavee" class="custmodalsavee btn btn-xs btn-success">Save</button>{{Form::close()}}</div>';
my_row.html(my_html);
$('div#dataaa').append(my_row);
}else if(value1['type'] == 'textareafield'){
var my_row = $('<div class="row mt-20 ccin">');
var my_html = '{{ Form::open() }}<input name="product_id" id="product_id" type="hidden" value="{{$product->id}}"><input name="specification_id" id="specification_id" type="hidden" value="'+vvvid+'"><div class="col-md-4">'+value1.title+'</div>';
my_html += '<div class="col-md-6"><textarea id="longtext_dec" name="longtext_dec[]" placeholder="text area field" class="longtext_dec form-control"></textarea></div>';
my_html += '<div class="col-md-2"><button type="button" id="custmodalsavee" class="custmodalsavee btn btn-xs btn-success">Save</button>{{Form::close()}}</div>';
my_row.html(my_html);
$('div#dataaa').append(my_row);
}
script
<script>
$("body").on("click", ".custmodalsavee", function(e){
e.preventDefault();
$.ajax({
type: "post",
url: "{{ url('admin/addnewcustomsubspecifications') }}",
data: {
'_token': $('input[name=_token]').val(),
'product_id': $('#product_id').val(),
'specification_id': $("#specification_id").val(),
'text_dec': $('.text_dec').val(),
'longtext_dec': $('.longtext_dec').val(),
},
success: function (data) {
$('#custspacmsg').append('<span class="text-success">Custom Specification added successfully in your product!</span>').fadeIn().delay(3000).fadeOut();
console.log('done!');
$('.myButton').css('display', 'none');
$('.myButtonlong').css('display', 'none');
$('.removebtn').css('display', 'none');
},
error: function (data) {
console.log('Error!');
}
});
});
</script>
This is how my specification_id
will append:
<input name="specification_id" id="specification_id" value="66" type="hidden">
other one for example
<input name="specification_id" id="specification_id" value="67" type="hidden">
As you see id's are different but each time i save my data it uses first form specification_id 66
so when I attempt to save data with specification_id 67
it also saves for 66
.
PS: Maybe it worth to mention (not sure) that I have several script that runs on body
$("body").on("click", ".....", function(e){
Any idea?
Upvotes: 0
Views: 42
Reputation: 7128
I added
var form = $(this).closest('form');
and changed my specification_id
to:
'specification_id': $(this).closest('form').find('#specification_id').val(),
now it's working as it supposed to.
Upvotes: 1