I need to delete a column using ajax and laravel so far I have the following but it does not work for me thanks,
I send a route to the post, this route I get from the path attribute of the button
view()
<button id="eliminarArticulo" ruta = "{{URL::to('auto/eliminarArticulo')}}" onclick="eliminarArticulo({{$detalle_modelo->idetalleModelo}})" class="btn btn-outline btn-danger"><i class="fa fa-trash"></i></button>
js(ajax)
<script>
function eliminarArticulo(id) {
var ruta = $('#eliminarArticulo').attr('ruta');
var url = ruta+"/"+id;
// alert(url)
$.post({
url: url,
data: {id:id},
dataType: 'json',
success : function (res) {
// console.log(res);
}
});
}
</script>
Ruta
Route::group(['prefix' => 'auto'], function(){
Route::get('crear', 'AutoController#crear')->name('auto.crear');
Route::post('registrar', 'AutoController#registrar')->name('auto.registrar');
Route::get('listar', 'AutoController#listar')->name('auto.listar');
Route::get('editar/{cod}', 'AutoController#editar')->name('auto.editar');
Route::post('actualizar', 'AutoController#actualizar')->name('auto.actualizar');
Route::get('ver/{cod}', 'AutoController#ver')->name('auto.ver');
Route::post('guardarArticulo', 'AutoController#guardarArticulo')->name('auto.guardarArticulo');
Route::get('editarArticulo/{cod}', 'AutoController#editarArticulo')->name('auto.editarArticulo');
Route::post('actualizarArticulo', 'AutoController#actualizarArticulo')->name('auto.actualizarArticulo');
Route::post('ActualizarEstadoAuto/{cod}', 'AutoController#ActualizarEstadoAuto')->name('auto.ActualizarEstadoAuto');
Route::post('eliminarArticulo/{cod}', 'AutoController#eliminarArticulo')->name('auto.eliminarArticulo');
});
controlador
public function eliminarArticulo(Request $request){
return $request;
}
Please feel free to correct my grammar because it's terrible
This is how I made my ajax delete function.
When the user clicks on the button it will trigger the onclick event I'm passing in the Id with onclick="deleteRmaMarketplace({{ $rmaMarketplace->id }}).
Here is my button once the user clicks on the button it will call the deleteRmaMarketplace() function also note that I'm passing the MarketplaceId into the function parameters like this deleteRmaMarketplace({{ $rmaMarketplace->id }})
<button class="btn btn-danger rm-marketplace" type="button" onclick="deleteRmaMarketplace({{ $rmaMarketplace->id }})">
<i class="fa fa-trash"></i>
</button>`
Here is my route I'm using named routes because it's easier and I'll use the named routes for my JavaScript variable.
Route::post('/rma-returnType-delete', 'AdminSettings\AdminSettingsController#rmaReturnTypeDelete')->name('admin.rma.returnType.delete');
Here is the JavaScript function I made first I made a variable called deleteRmaReturnTypeUrl and I gave it the value of my route like this '{{ route('admin.rma.returnType.delete') }}'; I can use this for the Url in the ajax function next I made a variable called token and I gave it the value of the '{{ csrf_token() }}'; you'll need it because all post requests required the token in laravel
var deleteRmaReturnTypeUrl = '{{ route('admin.rma.returnType.delete') }}';
var token = '{{ csrf_token() }}';
function deleteRmaMarketplace(Id) {
$.ajax({
url: deleteRmaMarketplaceUrl,
type: 'post',
data: {Id: Id, _token: token},
dataType: 'json',
success:function () {
location.reload();
}
});
}
Here is my controller code
First, we check if the request is ajax and then I make a variable called $ticketId and give it the value of the Id we posted from the ajax request next I made a variable $ticket and I get the RmaReturnType where the id = to the ticketId variable then we delete the ticket by calling $ticket->delete() and then we return the response
public function rmaReturnTypeDelete(Request $request)
{
if ($request->ajax()) {
$ticketId = $request->input('Id');
$ticket = RmaReturnType::where('id', $ticketId)->firstOrFail();
$ticket->delete();
return response()->json($ticket);
}
return response()->json('error', 422);
}
Related
I have a counter in Ajax and I would like to save the value of the counter in a database so that the value of the counter continues to grow even when the user disconnects or refreshes the page. I use the laravel framework. how to do it, please?
Add the csrf_token() to somewhere on the blade view:
<input type='hidden' name='_token' value='{{ csrf_token() }}' id="csrf">
Add this route to your routes/web.php file:
Route::post('add-to-counter', 'YourController#add');
Your php script:
public function add()
{
// retrieve de counter value from the database:
$i = DB::table(...)->select(...)->get();
$i = ++$i;
// and save it to the database
return response()->json([
'data' => $i
]);
}
Then, create a ajax request:
let _token = $("#csrf").val();
$.ajax({
url: 'add-to-counter',
method: 'post',
data: {
'_token': _token,
}
success: function(response){
// What happens if the ajax request finishes successfully
alert("The current counter value is: "+response.data);
}
});
I need to post values from a table cell to a servet through Ajax but the response is returning a null. My Javascript is not that strong and I am certainly doing something wrong and need some direction. Below are my JSP and Ajax script:
**My JSP code**
<td><button type="button" class="btn btn-default btn-md" class="itemData" tableData="${item}" onclick="saveData()"><span class="glyphicon glyphicon-save"></span></button> </td>
**My Ajax code**
function saveData(){
$(document).ready(function(){
var dataID = $(this).attr('tableData');
$.ajax({
url : 'DataCtrlServlet',
type: 'Post',
data : dataID,
success : function(responseText) {
$('#submissionSuccessContainer').text(responseText);
}
});
return false;
});
}
Romve return false; and $(document).ready ,also you need to change the data format to pass the parameter correctly
function saveData(){
var dataID = $(this).attr('tableData');
$.ajax({
url : 'DataCtrlServlet',
type: 'Post',
data : {
dataID:dataID
},
success : function(responseText) {
$('#submissionSuccessContainer').text(responseText);
}
});
}
I have a button and when it's clicked, I basically want to load the Auth::user()->name and a date into the database. I can't figure out how to do an ajax call without passing in some sort of data. This is my ajax query.
var url2 = "/application-background-sent/{{$application->id}}";
$(document).ready(function(){
$('#bgSubmit').click(function(e){
$.ajax({
url: url2,
success: function(data) {
alert(data);
},
type: 'GET'
});
});
});
My route:
Route::get('/application-background-sent/{id}', 'ApplicationsController#backgroundSent');
My controller:
public function backgroundSent($id)
{
$application = Application::findOrFail($id);
$application->bg_check_submitted_by = Auth::user()->name;
$application->save();
return response()->json(['success'=>'Data is successfully added']);
}
Need to delete a record without using form tag. Just pass a id in url and get a id need to process.
Thanks
follow below steps
HTML
Delete
JS
$('a#btnDelete').on('click',function()
{
var thisElement = $(this);
$.ajax({
type: 'post',
url: "{{url('/')}}" + '/deleteRecord/'+thisElement.attr('thisRecordId'),
data : {'_token': '{!! csrf_token() !!}'},
async: false,
success:function(response)
{
console.log(response)
}
});
});
Route
Route::post('deleteRecord/{id}','YourController#deleteRecord');
Controller
public function deleteRecord($id)
{
$findRecord = ModelName::findOrFail($id);
$findRecord->delete();
return 'success';
}
hope you will find your solution !
Try like this
<a type="button" class="label label-danger" href="/user/delete/{{$user->id}}">Delete</a>
Controller method
public function getDelete($id)
{
echo $id;
}
I'm new in Js. This is my code:
<button class="btn btn-primary" data-id = "#item.Id" id="accept">Accept</button>
var tempId;
$('button.accept').click(function () {
tempId = $(this).attr('data-id')
$.ajax({
type: "POST",
url: "/TabRequest/AcceptRequest",
data: { 'id': tempId },
success: function (msg) {
}
});
})
As you can see I'm trying to post "data-id" to Action. When I click to to button, does nothing. Can anybody help me?
You need
$('button#accept')
instead of
$('button.accept')
Since accept is an ID for the button, use # as selector and you can use . for class selector.
you can see reference for jQuery seletors