【问题标题】:Code igniter show div else show a different div in a viewCodeigniter show div else 在视图中显示不同的 div
【发布时间】:2013-01-12 04:50:34
【问题描述】:

我想在我的视图中设置条件中的 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.
}



   }

【问题讨论】:

    标签: php jquery ajax codeigniter controller


    【解决方案1】:

    在成功和失败 div 中通过 css 设置不显示,然后执行以下操作:

    $.ajax({ 网址:“”, 类型:'POST', 数据:form_data, 数据类型:'json', 成功:功能(味精){ 如果(msg.res == 1) { $(".success").fadeIn(500).delay(2000).fadeOut(500); } 别的 { $(".failiour").fadeIn(500).delay(2000).fadeOut(500); } } });

    【讨论】:

      【解决方案2】:

      首先隐藏你的 div

      <div class = "success" style="display:none;">operation done successfully </div>
      <div class = "failiour" style="display:none;">operation done successfully </div>
      

      如果你多次获得reuests

      $.ajax({
          url: "",
          type: 'POST',
          data: form_data,
          dataType: 'json',
          success: function(msg) {
              if(msg.res  == 1)
              {
                  $(".success").show();
                  $(".failiour").hide();
              }
              else
              {
                  $(".failiour").show();
                  $(".success").hide();
              }
      
      
          }
      });
      

      【讨论】:

      • thanksssssssssssss 男孩,它就像一个魅力......而且它是最简单的方法......谢谢大家
      【解决方案3】:

      在您的脚本中,

      //supposing you are getting the result in response varible
      if(response == true) // simply write:  if(response)
      {
      document.getElementById('success').style.display='block';
      document.getElementById('failure').style.display='none';
      }
      else
      {
      document.getElementById('success').style.display='none';
      document.getElementById('failure').style.display='block';
      }
      

      //为div添加ID并根据您的要求添加样式块/无

      <div id="success" style="display:block;"></div>
      
      <div id="failure" style="display:none;"></div>
      

      【讨论】:

      • 我如何得到回复?因为我收到来自控制器的响应是 '1',它是 json 类型...是 javascript 将其作为响应...?
      • 我认为您在数据变量中得到了响应。只需检查 if(data)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多