I'm trying to pass ajax data to a "settings/addsection/{class_id}" route with parameter so as to call a controller method to log on laravel log file. but logging does not work. So the ajax didn't send data to the route. How do I fix this?.. Note that the $class_id which i passed to the hidden input has value as i have queried it from the database.
**JAVASCRIPT**
<script>
document.addEventListener('DOMContentLoaded' , function(){
var add = document.getElementById('addSection');
add.addEventListener('submit' , function(e){
e.preventDefault();
var class_id = document.getElementById('class_id').value;
var section = document.getElementById('name').value;
var token = document.getElementsByName('_token')[0].value;
var data = {
name:section,
_token:token
}
var xhr = new XMLHttpRequest();
xhr.open('POST', 'settings/addsection/' + class_id, true);
xhr.setRequestHeader('Content-Type' , 'application/json');
xhr.send(JSON.stringify(data));
})
})
</script>
**HTML**
form id = "addSection" method="POST" action=" ">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right"></label>
<div class="col-md-6">
<input id="name" name="name" type="text" class="form-control @error('name') is-invalid @enderror" value="" autocomplete="name" autofocus placeholder="A,B,C...Z">
<input type="hidden" id = "class_id" value = "">
@error('name')
<span class="invalid-feedback" role="alert">
<strong></strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary btn-block">
</button>
</div>
</div>
</form>
**this is the web.php where the routes are**
Route::middleware(['auth' , 'admin'])->group(function(){
Route::prefix('settings')->group(function(){
Route::post('addsection/{class_id}' , 'SectionController@store');
});
});
**SectionController**
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Section;
class SectionController extends Controller
{
public function store(Request $request , $class_id){
//do stuff here
Log::info(' it works');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire