我想问一下这个错误的原因是什么?当我检查检查元素以检查它时,它说console.log(xhr.status);的console.log(xhr.responseText);的console.log(thrownError);是我的错误。这里有什么问题?
我的观点
<h3> Product
<a href="#new_product" data-toggle="modal" ><input type="hidden" class="btn btn-primary"><span class = "fa fa-plus-square-o "></a>
<div class="modal fade" id="new_product" role="dialog" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add New Product</h4>
</div>
<form method="post" id="form-submit" enctype="multipart/form-data">
<!-- <form method="post" enctype="multipart/form-data" name = "select_design"action="<?php echo base_url(); ?>Administrator/new_product"> -->
<div class="modal-body">
<!-- <div class="form-group">
<label class="control-label ">Genre:</label>
<select name = "product_genre" class="select2_multiple " multiple="multiple" >
<option>Choose option</option>
<option value ="drama">Drama</option>
<option value ="action">Action</option>
<option value ="comedy">Comedy</option>
<option value ="horror">Horror</option>
</select>
</div> -->
<div class="form-group">
<label>Product Name</label>
<input type="text" class="form-control" id="product_name" name="product_name" placeholder="Name" value="<?php echo set_value('product_name'); ?>"> </div>
<div class="form-group">
<label>Product Description</label>
<input type="text" class="form-control" id="product_description" name="product_description" placeholder="Description" value="<?php echo set_value('product_description'); ?>">
</div>
<div class="form-group">
<label>Stocks</label>
<input type="number" class="form-control" id="quantity" name="quantity" placeholder="Quantity" value="<?php echo set_value('quantity'); ?>">
</div>
<div class="form-group">
<label>Product Image</label>
<input type="file" required class="form-control" id="userfile" name="userfile">
</div>
<div class="form-group">
<label>Product Price</label>
<input type="number" min="1" class="form-control" id="product_price" name="product_price" placeholder="Price" value="<?php echo set_value('price'); ?>">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Register</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
我的js
<script>
$("#form-submit").on('submit',function(event){
// $.LoadingOverlay("show");
var form = new FormData(document.getElementById("form-submit"));
$.ajax({
url: base_url + "Administrator/new_product",
type: "POST",
data: form,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function(data) {
var result = JSON.parse(data);
//alert(data);
if(result===1)
{
swal({
type: 'success',
html: 'New Product Added',
timer: 2000,
})
setTimeout(function() {
document.location.href= base_url + "Administrator/view_products";
}, 2000);
}
else
{
swal({
type: 'error',
html: result
});
}
// $.LoadingOverlay("hide");
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(xhr.responseText);
console.log(thrownError);
}
})
event.preventDefault();
});
</script>
我的控制器
public function new_product()
{
// $data['new_product'] = $this->AdministratorModel->getCourses();
$config['upload_path'] = './products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('product_name', ' Name', 'required|trim');
$this->form_validation->set_rules('product_description', ' Name', 'required|trim');
$this->form_validation->set_rules('quantity', 'stocks', 'required|trim');
$this->form_validation->set_rules('product_price', 'price', 'required|trim');
$this->form_validation->set_rules('userfile', 'image', 'callback_handle_upload');
if ($this->form_validation->run() == FALSE)
{
echo json_encode(validation_errors());
}
else
{
$products = array(
'product_name' => strip_tags($_POST['product_name']),
'product_description' => strip_tags($_POST['product_description']),
'product_category' => "Drama",
'product_quantity' => strip_tags($_POST['quantity']),
'product_image' => $this->input->post('userfile'),
'upload_data' => $this->upload->data(),
'product_price' => strip_tags($_POST['product_price'])
);
$this->AdministratorModel->addProduct($products);
echo json_encode(1);
}
}
我的模特
public function addProduct($products)
{
$products = array(
'product_name' => strip_tags($_POST['product_name']),
'product_description' => strip_tags($_POST['product_description']),
'product_category' => strip_tags($_POST['product_genre']),
'product_quantity' => strip_tags($_POST['quantity']),
'product_image' => $this->input->post('userfile'),
'product_image' => $this->upload->data('file_name'),
'product_price' => strip_tags($_POST['product_price'])
);
$this->db->insert('products',$products);
}