gpt4 book ai didi

javascript - 如何使用 codeigniter 对 vehicle_number 和日期进行表单验证/数据限制

转载 作者:行者123 更新时间:2023-11-28 23:26:18 26 4
gpt4 key购买 nike

我不在这里包括表单输入等,因为我的代码可以正常工作。我想要一个像这个查询这样的表单验证是如果“车辆 1”在数据库中出现 5 次并且在同一交货日期选择交货表。警告车辆 1 只需要在该日期交付 5 次的错误。”

数据库:
车辆名称 |交货日期
车辆 1 2016/8/29
车辆 1 2016/8/29
车辆 1 2016/8/29
车辆 1 2016/8/29
车辆 1 2016 年 8 月 29 日

在第 6 个输入
输入:车辆 1
输入:2016 年 8 月 29 日

我的输入没有问题。我只想验证如果“车辆 1”在 2016 年 8 月 29 日在交货表上存在 5 次,则您不能再添加任何交货。我将如何编写代码?”

Controller

private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;

if($this->input->post('client_firstname') == '')
{
$data['inputerror'][] = 'client_firstname';
$data['error_string'][] = 'First name is required';
$data['status'] = FALSE;
}

if($this->input->post('client_lastname') == '')
{
$data['inputerror'][] = 'client_lastname';
$data['error_string'][] = 'Last name is required';
$data['status'] = FALSE;
}

if($this->input->post('client_contact') == '')
{
$data['inputerror'][] = 'client_contact';
$data['error_string'][] = 'Customer contact is required';
$data['status'] = FALSE;
}

if($this->input->post('client_address') == '')
{
$data['inputerror'][] = 'client_address';
$data['error_string'][] = 'Address is required';
$data['status'] = FALSE;
}

if($this->input->post('tracking_no') == '')
{
$data['inputerror'][] = 'tracking_no';
$data['error_string'][] = 'Tracking number is required';
$data['status'] = FALSE;
}

if($this->input->post('vehicle_id') == '')
{
$data['inputerror'][] = 'vehicle_id';
$data['error_string'][] = 'Vehicle Assignation Required';
$data['status'] = FALSE;
}


if($this->input->post('delivery_status') == '')
{
$data['inputerror'][] = 'delivery_status';
$data['error_string'][] = 'Delivery status is required';
$data['status'] = FALSE;
}


if($this->input->post('delivery_date') == '')
{
$data['inputerror'][] = 'delivery_date';
$data['error_string'][] = 'Delivery Date is required';
$data['status'] = FALSE;
}


if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}

模型

public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}

在我的脚本上

function add_delivery()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Add Delivery'); // Set Title to Bootstrap modal title
}

function save()
{
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled',true); //set button disable
var url;

if(save_method == 'add') {
url = "<?php echo base_url('index.php/delivery/ajax_add')?>";
$('#btnSave').text('Add Delivery');
} else {
url = "<?php echo base_url('index.php/delivery/ajax_update')?>";
}

// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{

if(data.status) //if success close modal and reload ajax table
{
$('#modal_form').modal('hide');
reload_table();
alert('Successfully Added');
}
else
{
for (var i = 0; i < data.inputerror.length; i++)
{
$('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('Save Delivery'); //change button text
$('#btnSave').attr('disabled',false); //set button enable


},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave').text('Save Delivery'); //change button text
$('#btnSave').attr('disabled',false); //set button enable

}
});

最佳答案

在模型中

function getVeichleCount($vehicle_name, $delivery_date)
{
$query = $this->db->query("SELECT count(id) FROM table_name WHERE vehicle_name = '$vehicle_name' AND delivery_date = $delivery_date ");
$result = $query->result_array();
return $result;
}

在回调函数中

$count = $this->model_name->getVeichleCount($vehicle_name, $delivery_date)[0];
if ($count >= 5) {
# count already have 5 records
# your error goes here
}

关于javascript - 如何使用 codeigniter 对 vehicle_number 和日期进行表单验证/数据限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com