PIC I want to ensure that when I hit enter, the input data is successfully saved to the database.
The Controller:
public function saveData(Request $request){
$data = Data::create([
'user' => Auth::user()->id,
'receiver' => $request->receiver_id,
'content' => $request->message,
'is_Saved' => 0,
]);
$data->save();
}
The form:
<div class="card-footer">
<form id="data_form">
<div class="input-group">
<div class="input-group-append">
<span class="input-group-text attach_btn"><i class="fas fa-paperclip"></i></span>
</div>
<input type="text" name="message" id="message_input" class="form-control type_msg" placeholder="Type your message...">
</div>
</form>
The ajax code:
$(document).on('keyup', '.input-group input', function (e) {
var message = $(this).val();
var url = '{{ url('data') }}';
// check if enter key is pressed and message is not empty also receiver is selected
if (e.keyCode == 13 && message != '' && receiver_id != '') {
$(this).val(''); // empty the text box on pressing enter
var datastr = "receiver_id=" + receiver_id + "&message=" + message;
$.ajax({
type: "post",
url: '/data',
data: datastr,
cache: false,
success: function (data) {
console.log(data)
},
error: function (jqXHR, status, err) {
},
complete: function () {
}
})
}
})
The Route:
Route::post('data', [ConsultationController::class, 'saveData']);
However, the data is not being saved in the database without any error notification. How can this issue be resolved? Please Help :)
I attempted deleting the If statement in the ajax code, which resulted in a 500 Internal Server ErrorPIC:
POST http://127.0.0.1:8000/data 500 (Internal Server Error)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
(anonymous) @ consultation?message=adfadf:399
dispatch @ jquery.min.js:2
v.handle @ jquery.min.js:2