ajax php 表格 删除,php – 使用laravel 5中的ajax从表中删除记录

我想使用ajax删除记录.

视图

@foreach( $products as $product )

{{ $product->code }}{{ $product->name }}{{ $product->display }}

<?php $time = date('d M, Y h:i:s A', strtotime($product->created_at)); ?>

{{ $time }}

{!! Form::button( '', ['type' => 'submit', 'class' => 'delete text-danger deleteProduct','id' => 'btnDeleteProduct', 'data-id' => $product->id ] ) !!}

{!! Form::close() !!}

@endforeach

调节器

public function destroy( $id, Request $request ) {

$product = Product::findOrFail( $id );

if ( $request->ajax() ) {

$product->delete( $request->all() );

return response(['msg' => 'Product deleted', 'status' => 'success']);

}

return response(['msg' => 'Failed deleting the product', 'status' => 'failed']);

}

ajax删除

$('.deleteProduct').on('click', function(e) {

var inputData = $('#formDeleteProduct').serialize();

var dataId = $('#btnDeleteProduct').attr('data-id');

$.ajax({

url: '{{ url('/admin/products') }}' + '/' + dataId,

type: 'POST',

data: inputData,

success: function( msg ) {

if ( msg.status === 'success' ) {

toastr.success( msg.msg );

setInterval(function() {

window.location.reload();

}, 5900);

}

},

error: function( data ) {

if ( data.status === 422 ) {

toastr.error('Cannot delete the category');

}

}

});

return false;

});

**编辑1 **:

如果我只返回console.log(msg),这就是我得到的

Object {

id: "1",

code: "PROD-521420",

name: "Testing the product name",

category_id: "3",

short_description: "This is the short description"…

}

category_id: "3"

code: "PROD-521420"

created_at: "2015-06-07 23:00:31"

deleted_at: null

description: "This is the long description"

discount_price: "125.00"

display: "Enabled"

id: "1"

meta_description: "This is the meta description"

meta_keywords: "This is the meta keywords"

meta_title: "This is the meta title"

name: "Testing the product name"

price: "150.00"

short_description: "This is the short description"

updated_at: "2015-06-08 10:04:26"

问题是,这会删除产品,但只删除第一行而不是单击的那一行.

我想删除点击的产品.

有人可以帮忙吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值