使用ajax更新php foreach循环中的td值
我正在将数据从MySql查询发送到blade.php。我正在将这些数据传递到一个表中 然后我有一个php foreach循环,它迭代查询中的每一行 我试图用ajax更新td中的各行,但无论更新哪一行,它总是采用最后一行的值,并将所有行更新为相同的值 它也没有将正确的id传递给ajax请求。它传递第一行id,但使用最后一行数据进行更新 你能帮我找到我的问题吗 这里的想法是能够更新单个行。因此,我想通过ajax请求将name=“phone\u id”隐藏输入的值传递给我的php函数。当前,它只传递隐藏输入字段中的第一行值 首先是形式使用ajax更新php foreach循环中的td值,php,jquery,ajax,laravel,foreach,Php,Jquery,Ajax,Laravel,Foreach,我正在将数据从MySql查询发送到blade.php。我正在将这些数据传递到一个表中 然后我有一个php foreach循环,它迭代查询中的每一行 我试图用ajax更新td中的各行,但无论更新哪一行,它总是采用最后一行的值,并将所有行更新为相同的值 它也没有将正确的id传递给ajax请求。它传递第一行id,但使用最后一行数据进行更新 你能帮我找到我的问题吗 这里的想法是能够更新单个行。因此,我想通过ajax请求将name=“phone\u id”隐藏输入的值传递给我的php函数。当前,它只传递隐
<div class="row">
<div class="col-12 col-md-8 offset-md-2 text-center mt-30 rename">
<table class="table">
<thead>
<tr>
<th scope="col">Number</th>
<th scope="col">Description</th>
<th scope="col">Balance</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($phones as $phone)
<tr>
<th scope="row"><input value="{{$phone->phone_account}}" type="text" name="phone_account{{$phone->phone_id}}"></th>
<td><input value="{{$phone->description}}" type="text" name="description{{$phone->phone_id}}"></td>
<td><input value="{{ $phone->balance }}" disabled></td>
<td>
<input type="text" value="{{ $phone->phone_id }}" name="phone_id" hidden>
<input id="VerifyBtn" onclick="postData()" type="submit" class="btn btn-profile" value="Update">
<a href="{{ route('phonedelete', ['id'=>$phone->phone_id]) }}" role="button" class="btn btn-profile">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
更新功能
public function update_phone(Request $request){
$id = $request->id;
$user_id = $request->user_id;
$phone_acc = $request->phone_account;
$phone_desc = $request->description;
$date = date("Y-m-d H:i:s");
DB::table('phone_details')
->where('user_id', $user_id)
->where('phone_id', $id)
->update(['phone_account' => $phone_acc,
'description' => $phone_desc,
'updated_at' => $date]);
echo 'success';
}
您可以让您的生活更轻松,只需将ID设置为与每个TR相对,就可以清理一些输入,包括清除隐藏的phone_ID输入。我将
名称
更改为类
,因为对我来说,它在jQuery中看起来更干净,如下所示:
@foreach ($phones as $phone)
<tr data-phoneid="{{$phone->phone_id}}">
<th scope="row"><input value="{{$phone->phone_account}}" type="text" class="phone-account"></th>
<td><input value="{{$phone->description}}" type="text" class="phone-description"></td>
<td><input value="{{ $phone->balance }}" disabled></td>
<td>
<input id="VerifyBtn" onclick="postData(this)" type="submit" class="btn btn-profile" value="Update">
<a href="{{ route('phonedelete', ['id'=>$phone->phone_id]) }}" role="button" class="btn btn-profile">Delete</a>
</td>
</tr>
@endforeach
将id分配给tr并在ajax中使用。不要像那样将PHP直接插入JS
var user\u id=@json($phone->user\u id)代码>更安全。您也可以使用$parent.data(“phoneid”)
访问数据属性,这些属性更易于阅读。谢谢@miken32-我从中学到了一些新东西谢谢@Kinglish,您的回答帮助了我。您只需在电话帐户和电话描述之前添加一个句点,即可指定要调用的类。非常感谢。
@foreach ($phones as $phone)
<tr data-phoneid="{{$phone->phone_id}}">
<th scope="row"><input value="{{$phone->phone_account}}" type="text" class="phone-account"></th>
<td><input value="{{$phone->description}}" type="text" class="phone-description"></td>
<td><input value="{{ $phone->balance }}" disabled></td>
<td>
<input id="VerifyBtn" onclick="postData(this)" type="submit" class="btn btn-profile" value="Update">
<a href="{{ route('phonedelete', ['id'=>$phone->phone_id]) }}" role="button" class="btn btn-profile">Delete</a>
</td>
</tr>
@endforeach
function postData(el) {
let $parent = $(el).closest('tr');
var id = $parent.attr('data-phoneid');
var phone_account = $parent.find('phone-account').val();
var description = $parent.find('phone-description').val();
var user_id = @json($phone->user_id);
$.ajax({
........
}