所以我想通过 AJAX 从我的js文件中发送我要从我的复选框中删除的数据到我的控制器。它会被传递,但是每当我用count()
或array_length()
检查时,长度总是1,尽管输出显示的变量多于1。
jsondata
当我使用dd dd显示时,它只显示1个数组长度
表:
<?= form_open('/delete', ['class' => 'deleteBatch']); ?>
<div class="w-fit">
<div class="grid grid-cols-2 gap-x-1 m-3">
<button data-toggle="modal" data-target="#createModal" class="inline-flex items-center justify-center rounded-md border border-transparent bg-primary-600 px-3 py-1 text-base font-medium text-white hover:bg-primary-700">New</button>
<button @click="deleteCheck()" class="inline-flex items-center justify-center rounded-md border border-transparent bg-primary-600 px-3 py-1 text-base font-medium text-white hover:bg-primary-700">Delete</button>
</div>
</div>
<div class="mb-3">
<?php $msg = session()->getFlashdata('message') ?>
<?php if ($msg) { ?>
<?php
if (is_array($msg)) {
?>
<div class="bg-red-200 w-auto bg-opacity-50 border border-red-400 text-red-700 px-3 py-2 rounded relative" role="alert">
<?php foreach (session()->getFlashdata('message') as $row) : ?>
<p class="block sm:inline"><?= $row; ?></p>
<br>
<?php endforeach; ?>
</div>
<?php
} else {
?>
<div class="bg-green-200 w-auto bg-opacity-50 border border-green-400 text-green-700 px-3 py-2 rounded relative" role="alert">
<span class="block sm:inline"><?= $msg; ?></span>
</div>
<?php
} ?>
<?php
} ?>
</div>
<div id='table'>
<table id="example" class="table-auto border-2 border-gray-400 cell-border">
<thead class="bg-gray-400">
<tr class="text-black">
<th>
<input id="selectAll" name="selectAll " type="checkbox">
</th>
<th>Action</th>
<th>Project Name</th>
<th>Client</th>
<th>Project Start</th>
<th>Project End</th>
<th>Status</th>
</tr>
</thead>
<tbody class="text-black">
<?php //dd($project);
?>
<?php foreach ($project as $row) : ?>
<tr>
<td><input class="selectId" name="selectId" value="<?= $row->project_id; ?>" type="checkbox"><?= $row->project_id; ?></td>
<td><a href="/edit">edit</a></td>
<td><span id="projectName" name="projectName"><?= $row->project_name; ?></span></td>
<td><span id="clientName" name="clientName"><?= $row->client_name; ?></span></td>
<td id="dateProjectStart"><span id="dateProjectStart" name="dateProjectStart">{{translateMonth(<?= strtotime($row->project_start); ?>)}}</span></td>
<td id="dateProjectEnd"><span id="dateProjectEnd" name="dateProjectEnd">{{translateMonth(<?= strtotime($row->project_end); ?>)}}</span></td>
<td><span id="projectStatus" name="projectName"><?= $row->project_status; ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p>{{message}}</p>
</div>
<?= form_close(); ?>
the js(im using vue.js):
const crud = Vue.createApp({
data(){
return{
'project_id' : "",
}
},
methods:{
create(e){
// console.log('hello')
document.getElementById("createForm").submit();
},
translateMonth(date){
const date2 = new Date(date*1000)
const month = ["Jan","Feb","Mar", "Apr","Mei", "Jun","Jul", "Agu","Sep","Oct","Nov","Dec"]
const dateFinal = date2.getDate() + ' ' + month[date2.getMonth()] + ' ' + date2.getFullYear()
return dateFinal
},
deleteCheck(e){
$('.deleteBatch').submit(function(e){
e.preventDefault();
let dataCount = $('.selectId:checked');
if(dataCount.length == 0){
Swal.fire({
icon: 'warning',
title: 'Warning',
text: 'Please select at least one project',
confirmButtonText: 'Ok',
confirmButtonColor: '#2563eb',
})
}else{
Swal.fire({
title: 'Are you sure you want to delete this project?',
text: dataCount.length + ' project will be deleted',
reverseButtons: true,
showCancelButton: true,
denyButtonText: `No`,
denyButtonColor: '#64748B',
confirmButtonText: 'Yes',
confirmButtonColor: '#2563eb',
}).then((result) => {
/* Read more about isConfirmed, isDenied below */
if (result.isConfirmed) {
$.ajax({
type: "post",
url: '/delete',
dataType: 'json',
data: $(this).serialize(),
success:function(response){
if (response.status == 200){
Swal.fire({
icon: 'success',
title: 'Project deleted successfully',
confirmButtonText: 'Ok',
confirmButtonColor: '#2563eb',
})
window.location.replace('/project')
}
}
})
} else if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
})
}
return false;
})
//
}
}
})
crud.mount('#crud')
控制器:
public function delete()
{
$session = \Config\Services::session();
$projectModel = new ProjectModel();
$selectedId = $this->request->getVar('selectId');
$data = [
'selectId' => $selectedId
];
$dataCount = count($data);
dd($dataCount);
}
我已经改变了$this->request->getVar('selectId');
到$this->request->getPost('selectId')
仍然没有运气,已经尝试了json_decode
太,但仍然相同
1条答案
按热度按时间bvn4nwqk1#
controller.php:
在这一部分中,您将
$data
设置为一个数组,其中包含一个元素(selectId
)。需要向数组中添加更多元素,使其更长。count()
在这种情况下输出正确的值(1)。