我有带有一些文本输入和文件输入的HTML表单。我想使用jquery表单插件提交表单,它工作正常,但问题是当我在文件输入中选择没有文件时,文件输入字段成为$_POST
空值的一部分,我不希望它在$_POST
数组中。
请帮忙。
这是我正在使用 CodeIgniter 的代码 - @Let's Code
public function saveDataSourceAttributesValues() {
$data_source_id = $this->input->post('data_source_id');
$attributes_values_row_id = $this->input->post('row_id');
$text_attributes = $this->input->post(); // Text Input fields
$images_attributes = $_FILES; // Files Input field
if ($attributes_values_row_id == '') {
$last_row_id = $this->DataSourceModel->getDataSourceAttributesValuesLastRowId($data_source_id);
if ($last_row_id) {
$row_id = (int) $last_row_id + 1;
} else {
$row_id = 1;
}
} else {
//Edit case
$row_id = $attributes_values_row_id;
}
if (!empty($text_attributes)) {
foreach ($text_attributes as $attribute_id => $value) {
if (is_numeric($attribute_id)) {
$temp_data = array(
'data_source_id' => $data_source_id,
'data_source_attribute_id' => $attribute_id,
'value' => $value,
'row_id' => $row_id,
'created' => date('Y-m-d H:i:s')
);
if ($attributes_values_row_id == '') {
$this->CommonModel->insert('data_source_attribute_value', $temp_data);
} else {
$this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data);
}
}
}
}
if (!empty($images_attributes)) {
$this->load->library('upload');
$config['upload_path'] = './data_sources/images/';
$config['allowed_types'] = '*';
$config['max_size'] = '10000';
//$config['overwrite'] = TRUE;
//$config['remove_spaces'] = TRUE;
foreach ($images_attributes as $attribute_id => $value) {
if (is_numeric($attribute_id)) {
$config['file_name'] = str_replace(' ', '_', $_FILES[$attribute_id]['name']);
$image_path = $config['upload_path'] . '/' . $config['file_name'];
$this->upload->initialize($config);
if ($this->upload->do_upload($attribute_id)) {
$temp_data = array(
'data_source_id' => $data_source_id,
'data_source_attribute_id' => $attribute_id,
'value' => $image_path,
'row_id' => $row_id,
'created' => date('Y-m-d H:i:s')
);
if ($attributes_values_row_id == '') {
$this->CommonModel->insert('data_source_attribute_value', $temp_data);
} else {
$this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data);
}
}
}
}
}
$response['response'] = 200;
$response['message'] = 'Data source attributes values has been saved successfully.';
$response['error'] = '';
$response['data'] = '';
}