我想在我的视图中设置条件中的 div,但我不知道该怎么做。如果控制器的请求对他来说是真实的,那么成功 div 出现,否则失败 div。当时我只是在警报框中打印真假。这是我的观点:
<div class = "success">operation done successfully </div>
<div class = "failiour">operation done successfully </div>
//these are the two divs on which i want to apply a condition
<form>
</form>
<script type="text/javascript">
$('#btn').click(function() { // $("#form").serialize()
var item_name = $('#item_name').val();
var cat_id = $('#selsear').val();
if (!item_name || item_name == 'Name') {
alert('Please enter Category Name');
return false;
}
var form_data = {
item_name: $('#item_name').val(),
cat_id: $('#selsear').val(),
};
$.ajax({
url: "<?php echo site_url('itemsController/additems'); ?>",
type: 'POST',
data: form_data,
dataType: 'json',
success: function(msg) {
if(msg.res == 1)
{
alert(true);
}
else{
alert(false);
}
}
});
return false;
});
</script>
我的控制器
function additems(){
//getting parameters from view
$data = array(
'item_name' => $this->input->post('item_name'),
'cat_id' => $this->input->post('cat_id')
);
//$is_ajax = $this->input->post('ajax'); //or use this line
//$this->input->is_ajax_request();
$result = array();
$this->load->model('itemsModel');
$query = $this->itemsModel->addItemstoDB($data);
if ($query){ //&& any other condition
$result['res'] = 1;//process successful - replace 1 with any message
}
else
{
$result['res'] = 0;//process failed - replace 0 with any message
}
echo json_encode($result);//at the end of the function.
}
}