phpcodeignitercodeigniter-3

Alert box is not showing inside codeigniter controller


In this controller, the alert box is not working inside the loop.The if condition is working; the only problem is with the alert box,but it's not showing the dialog box. Please help. I tried so many times.

Controller Erp_c

function tabl()
{
   $result['query2']=$this->erp_m->getregion();
   
   $this->load->view('head1');    
        $this->load->view('header3');
   $this->load->view('userregionview',$result);
}    

function userregioninsert($user)
        {
             
            if($this->input->post())
            $m1=$this->input->post('reg');
        //    print_r($m1);die;
                    
              $result['query']=$this->erp_m->insertregion($m1,$user);
              $result['query2']=$this->erp_m->getregion();
        //      print_r($result['query']);die;
                  if(($result['query'])>0)
                  {
                      
          ?>
                     <script type=text/javascript>alert("Region already added");</script>
                 
        <?php
        
        }
        else
        {
        
           ?>
                     <script type=text/javascript>
            alert("Name Available");
        </script>

        <?php
        
                  $this->erp_m->insertregion2($m1,$user);
                     
                  }
                  
                  redirect('Erp_c/tabl');

                } 
    
  

model erp_m

  function getregion()
      {
          
            $query2=$this->db->get('region3');
           return $query2->result();
      }
       function insertregion($m1,$user)
      {
           $this->db->where('region',$m1);
           $res=$this->db->get('region3');
           
            $num=$res->num_rows();        
          return $num;             
      }
      
      
 function insertregion2($m1,$user)
        {
            $data=array('region'=>$m1,'user'=>$user);
            $this->db->insert('region3',$data);

        }

Solution

  • You are missing with "".

    This <script type=text/javascript> Should come as like this <script type="text/javascript">

    <script type="text/javascript">
        alert("Region already added");
    </script>
    
    <script type="text/javascript">
        alert("Name Available");
    </script>
    

    And Check empty with empty() function

    Edit 01

        function insertregion($m1)
        {
            $query = $this->db->query("SELECT * FROM table_name WHERE region='$m1' ");//change table name
            $result = $query->result_array();
            $count = count($result);
            return $count;
        }
    

    And instead of using this if ( $this->input->post() ) use if(isset($_POST['reg']))

    So your Final Answer would be

        function userregioninsert($user)
        {
    
            if(isset($_POST['reg']))
            {
                $m1 = $this->input->post('reg');
    
                $count = $this->erp_m->insertregion($m1);
                $result['query2'] = $this->erp_m->getregion();
    
                if ($count==1)
                {
                    ?>
                    <script type="text/javascript">
                        alert("Region already added");
                    </script>
                <?php
                }
                else
                {
                    ?>
                    <script type="text/javascript">
                        alert("Name Available");
                    </script>
                    <?php
                    $this->erp_m->insertregion2($m1, $user);
                }
            }
            else
            {
                redirect('Erp_c/tabl');
            }           
    
        }