I'm trying to save data to database with ajax, i am using the exact same way for to ajax request one of them working another one no.
here is my ajax that doesn't work.
script
<script>
$( document ).ready( function() {
$("#modalsave1").click(function(e){
e.preventDefault();
$.ajax({
type: "post",
url: "{{ url('admin/addnewsuboptionprodcreat') }}",
data: {
'_token': $('input[name=_token]').val(),
'subopttitle': $('input[name=subopttitle]').val(),
'opt_id': $('#opt_id').val(),
'subopt_price': $('#subopt_price').val(),
'optstat_id': $('#optstat_id').val(),
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
</script>
blade form
{{Form::open()}}
// get parent
{{ Form::label('opt_id', 'Parent Option') }}
<select class="form-control" id="opt_id" name="opt_id">
<option value="">Select</option>
@foreach($options as $option)
<option value="{{ $option->id }}">{{ $option->title }}</option>
@endforeach
</select>
//get title
{{ Form::label('subopttitle', 'Name') }}
{{ Form::text('subopttitle', null, array('id' => 'subopttitle', 'class' => 'form-control')) }}
//get price
{{ Form::label('subopt_price', 'Price') }}
{{ Form::text('subopt_price', null, array('id' => 'subopt_price', 'class' => 'form-control')) }}
//get status
{{ Form::label('optstat_id', 'Include filters?') }}
<select class="form-control" id="optstat_id" name="optstat_id">
<option value="">Select</option>
@foreach($statuses as $status)
<option value="{{ $status->id }}">{{ $status->title }}</option>
@endforeach
</select>
//save it
<button type="submit" id="modalsave1" class="modalsave1 btn btn-primary">Save</button>
{{Form::close()}}
and here is my controller
public function addnewsuboptionprodcreat(Request $reqs)
{
$add = Suboption::create([
'title' => $reqs->subopttitle,
'option_id' => $reqs->opt_id,
'price' => $reqs->subopt_price,
'status_id' => $reqs->optstat_id,
]);
if($add){
Session::flash('success', 'Your Sub-option saved successfully.');
}else{
Session::flash('danger', 'Your Sub-option didn\'t save successfully.');
}
}
route
Route::post('/addnewsuboptionprodcreat', 'ProductController@addnewsuboptionprodcreat')->name('addnewsuboptionprodcreat');
I wonder, exact same method just different inputs
works and this not.
Error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mysite`.`suboptions`, CONSTRAINT `suboptions_option_id_foreign` FOREIGN KEY (`option_id`) REFERENCES `options` (`id`)) (SQL: insert into `suboptions` (`title`, `price`, `updated_at`, `created_at`) values (Yellow, 5000, 2018-05-07 07:57:16, 2018-05-07 07:57:16))
as you see in last line of the error i only get title
price
and timestamps
but nothing for parent
and status
any idea?