I have data which will append to blade view codes below
and each of them is warped into a form, the issue is data that will send to back-end always comes from first form (somehow other forms data will be ignored).
How my forms look like:
Sending data from first form ID = 7
Sending data from second form Still ID 7
Code
<script defer>
$(document).ready(function() {
$("body").on("click", ".savedynspecto", function(e){
var form = $(this).closest('form');
var id = form.find('input[name="product_id"]').val();
// e.preventDefault();
$.ajax({
type: "post",
url: '{{ url('admin/spacssendto') }}',
data: {
'_token': $('input[name=_token]').val(),
'product_id': id,
'subspecifications': $(this).closest('form').find('select.subspecifications').val()
},
success: function (data) {
alert('Specifications added successfully.');
},
error: function (data) {
console.log('Error!');
}
});
});
});
</script>
Controller
public function spacssendto(Request $request) {
$testingdata = $this->validate($request, array(
'product_id' => 'required',
'subspecifications' => 'required',
));
$product = Product::find($request->product_id);
$product->subspecifications()->sync($request->subspecifications, false);
return response()->json($testingdata);
}
HTML output of my forms
<tr>
<form method="POST" action="http://site.pp/admin/products/15/edit" accept-charset="UTF-8">
</form>
<input name="_token" value="DLrcOa0eOm90e4aaGSYp2uCeiuKtbGCT9fCOUP16" type="hidden">
<input name="product_id" id="product_id" value="15" type="hidden">
<td>Graphic</td>
<td>
<select class="subspecifications form-control tagsselector" id="subspecifications" name="subspecifications[]" multiple="multiple">
<option value="7">Intel 920</option>
</select>
</td>
<td class="text-center">
<button type="button" id="savedynspecto" class="savedynspecto btn btn-md btn-success"><i class="fa fa-check"></i> Save</button>
</td>
</tr>
Note:
What makes me curious is why my form closed right after it opened!
<tr>
<form method="POST" action="http://site.pp/admin/products/15/edit" accept-charset="UTF-8">
</form>
...
here is how my row will append to blade
var my_row = $('<tr>');
var my_html = '{{ Form::open() }}<input name="product_id" id="product_id" type="hidden" value="{{$product->id}}"><td>'+value1.title+'</td>';
my_html += '<td><select class="subspecifications form-control tagsselector" id="subspecifications" name="subspecifications[]" multiple="multiple">'+helpers+'</select></td>';
my_html += '<td class="text-center"><button type="button" id="savedynspecto" class="savedynspecto btn btn-md btn-success"><i class="fa fa-check"></i> Save</button></td>{{Form::close()}}</tr>';
my_row.html(my_html);
$('#addcustomstr').append(my_row);
As you see my {{ Form::open() }}
is before first <td>
and {{Form::close()}}
is right before </tr>
but why is closing right after it's open?
Any idea?