Welcome Guest, Not a member yet? Register   Sign In
validation and pass an id in url to view from controller
#1

Hello
sorry for my bad english. i really need help  Sad
i am creating a simple help desk and i have one question please answer me.

1: go to new ticket ( ex: localhost/helpdesk/index.php/ticket/show/10 ).

for number 1 i have a function ( public show ) and display all replies in id 10.

2: and show/10 has a form for reply your comment. ( screenshot : http://s6.uplod.ir/i/00880/op3jnntpy1lu.jpg )

then i need validation form when users submit new reply. i wrote validation rules, but i can't pass message error from controller to view (ticket/show/id).

PHP Code:
public function show()
 
   {
 
       $show = array();
 
       $id $this->uri->segment('3');
 
       $groups_id $this->ticket_model->get_groups_id($id);
 
       $show['show_ticket'] = $this->ticket_model->show($id$groups_id);

 
       foreach ($show['show_ticket'] as $show_value){
 
           $show_gorup_id $show_value['department_id'];
 
           $create_by_id $show_value['user_id'];
 
       }

 
       $show['show_ticket_comment'] = $this->ticket_model->get_reply($id);

 
       foreach ($this->aauth->get_user_groups() as $user_groups) {
 
           $user_groups_id $user_groups->id;
 
       }

 
       if (($this->aauth->is_member('Admin')) ||($user_groups_id == $show_gorup_id) || ($create_by_id == $this->aauth->get_user_id())) {
 
           $this->template->load('ticket/show_ticket'$show);
 
       }else{
 
           echo 'Access Denied';
 
       }
 
   
PHP Code:
  public function reply()
 
   {
 
       $this->form_validation->set_rules('comment','Comment','required|trim');
 
       if($this->form_validation->run() == FALSE){
 
           $ticket_id $this->input->post('ticket_id');
 
           //$show['show_ticket_comment'] = $this->ticket_model->get_reply($ticket_id);
 
          // $this->template->load('ticket/show/'.$ticket_id,array('errors' => validation_errors('','<br />')));
 
          // return;
 
           
            
// what code needs here ??????

 
       }else{
 
           if($_FILES['userfile']){
 
               $this->load->library('upload'$config);
 
               //$this->upload->initialize($config);
 
               if($this->upload->do_upload('userfile')){
 
                   $config['upload_path'] = './file_ticket';
 
                   $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|txt';
 
                   $config['max_size'] = 4000;
 
                   $config['max_width'] = 11024;
 
                   $config['max_height'] = 7168;
 
                   $upload_data $this->upload->data();
 
                   $file_name $upload_data['file_name'];

 
                   $reply_ticket['ticket_comment_id'] = '';
 
                   $reply_ticket['ticket_id'] = $this->input->post('ticket_id');
 
                   $reply_ticket['user_id'] = $this->aauth->get_user_id();
 
                   $reply_ticket['comment'] = $this->input->post('comment');
 
                   $reply_ticket['create_date'] = now();
 
                   $reply_ticket['file'] = $file_name;
 
                   $insert_ticket_comment $this->ticket_model->reply($reply_ticket);
 
                   if ($insert_ticket_comment){
 
                       $show = array();
 
                       $id $this->input->post('ticket_id');
 
                       $groups_id $this->ticket_model->get_groups_id($id);
 
                       $show['show_ticket'] = $this->ticket_model->show($id$groups_id);

 
                       redirect('ticket/show/'.$id);
 
                   }
 
               }else{
 
         //          $error = array('error' => $this->upload->display_errors());
echo '11';
 
           //        $this->load->view('show_ticket', $error);
 
               }
 
           }else{print_r($_POST);}
 
       }
 
   
Reply
#2

(This post was last modified: 05-15-2017, 10:59 PM by neuron.)

in Codeigniter best practice is to put form_validation in the same method. I mean you should merge your show() and reply() methods

PHP Code:
public function show($groups_id)
{
if(
$this->input->post()){ //check if user posted form

}
else{ 
//if nothing posted

}


To show errors in view :
PHP Code:
<?php echo form_error('field name''<div class="error">''</div>'); ?>


Try this way, if this does not help, post your view code also.
Reply
#3

please more explain. i have problem in reply function
Reply
#4

(This post was last modified: 05-15-2017, 11:36 PM by neuron.)

PHP Code:
public function show($groups_id)
{
if(
$this->input->post()){ //check if user posted form
 
         $this->form_validation->set_rules('comment','Comment','required|trim');
 
       if($this->form_validation->run() == FALSE){
 
           $ticket_id $this->input->post('ticket_id');
 
           $show['show_ticket_comment'] = $this->ticket_model->get_reply($ticket_id);
 
           $this->template->load('ticket/show/'.$ticket_id);
 
          // return;
 
           
            echo form_error
('comment''<div class="error">''</div>'); // you gonna put this code in your view file

 
       }else{
 
           if($_FILES['userfile']){
 
               $this->load->library('upload'$config);
 
               //$this->upload->initialize($config);
 
               if($this->upload->do_upload('userfile')){
 
                   $config['upload_path'] = './file_ticket';
 
                   $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|docx|txt';
 
                   $config['max_size'] = 4000;
 
                   $config['max_width'] = 11024;
 
                   $config['max_height'] = 7168;
 
                   $upload_data $this->upload->data();
 
                   $file_name $upload_data['file_name'];

 
                   $reply_ticket['ticket_comment_id'] = '';
 
                   $reply_ticket['ticket_id'] = $this->input->post('ticket_id');
 
                   $reply_ticket['user_id'] = $this->aauth->get_user_id();
 
                   $reply_ticket['comment'] = $this->input->post('comment');
 
                   $reply_ticket['create_date'] = now();
 
                   $reply_ticket['file'] = $file_name;
 
                   $insert_ticket_comment $this->ticket_model->reply($reply_ticket);
 
                   if ($insert_ticket_comment){
 
                       $show = array();
 
                       $id $this->input->post('ticket_id');
 
                       $groups_id $this->ticket_model->get_groups_id($id);
 
                       $show['show_ticket'] = $this->ticket_model->show($id$groups_id);

 
                       redirect('ticket/show/'.$id);
 
                   }
 
               }else{
 
         //          $error = array('error' => $this->upload->display_errors());
 
               echo '11';
 
           //        $this->load->view('show_ticket', $error);
 
               }
 
           }else{print_r($_POST);}
 
       }
}
else{ 
//if nothing posted
 
       $show['show_ticket'] = $this->ticket_model->show($id$groups_id);

 
       foreach ($show['show_ticket'] as $show_value){
 
           $show_gorup_id $show_value['department_id'];
 
           $create_by_id $show_value['user_id'];
 
       }

 
       $show['show_ticket_comment'] = $this->ticket_model->get_reply($id);

 
       foreach ($this->aauth->get_user_groups() as $user_groups) {
 
           $user_groups_id $user_groups->id;
 
       }

 
       if (($this->aauth->is_member('Admin')) ||($user_groups_id == $show_gorup_id) || ($create_by_id == $this->aauth->get_user_id())) {
 
           $this->template->load('ticket/show_ticket'$show);
 
       }else{
 
           echo 'Access Denied';
 
       }
}

Reply
#5

could you have a look my view
PHP Code:
<section class="panel">
 
    <?php
     
foreach ($show_ticket as $value){
 
    $id $value['ticket_id'];
 
    $create_by $value['username'];
 
    $create_by_id $value['id'];
 
    $department $value['name'];
 
    $subject $value['subject'];
 
    $status $value['active'];
 
    $create_date $value['create_date'];
 
    $create_date jdate('Y/m/j H:i',$create_date);
 
    $message $value['message'];
 
    ?>
    <header class="panel-heading">
    مشاهده تیکت<?php echo '#' $id?>
    </header>
    <?php echo $subject?>
    
    <table class="table table-striped table-advance table-hover">
    <thead>
         <tr>
             <th>ایجاد شده توسط</th>
             <th>واحد</th>
             <th>وضعیت</th>
             <th>تاریخ ایجاد</th>
         </tr>
    </thead>
         <tr>
             <td><?php echo $create_by?></td>
             <td><?php echo $department?></td>

             <td><?php if($status == 1){echo 'منتشر شده';}elseif($status == 0){echo 'منتشر نشده';} ?></td>
             <td><?php echo $create_date?></td>
         </tr>
         <?php ?>
     </table>
     <div id="message_ticket_orginal"><?php echo $message?></div>
     <button type="button" id="btn-reply1" class="btn btn-primary">پاسخ دادن</button>
     
<div id="reply_ticket">     
         <?php echo validation_errors(); ?>
         <?php echo form_open_multipart('ticket/reply'); ?>
    <div class="form-group">
        <label class="control-label col-sm-2">شرح درخواست</label>
        <div class="col-sm-10">
            <textarea class="form-control editor1" name="comment" id="editor1" rows="6"></textarea>
        </div>
    </div>
    <div class="form-group">
        <label for="userfile">آپلود تصویر</label>
        <input type="file" name="userfile" size="20" />
        <small>فایل های مجاز: txt,doc,gif,png,jpeg,jpg</small>
    </div>
    <input type="submit" value="ایجاد" class="btn btn-round btn-info">
    <a href="<?php echo base_url(); ?>index.php/ticket/all" class="btn btn-round btn-default">بازگشت</a>
    <input type="hidden" value="<?php echo $id?>" name="ticket_id">
    </form>
</div>

     <div id="#reply_list">
         <?php
         
foreach ($show_ticket_comment as $value_ticket_comment){
 
        $reply_id $value_ticket_comment['ticket_id'];
 
        $reply_create_by $value_ticket_comment['username'];
 
        $reply_comment $value_ticket_comment['comment'];
 
        $reply_date $value_ticket_comment['create_date'];
 
        $reply_date jdate('Y/m/j H:i',$reply_date);
 
        $reply_file $value_ticket_comment['file'];
 
        ?>

        <div id="reply_header">
            <div id="reply_date"><?php echo $reply_date?></div>
            <div id="reply_create"><?php echo $reply_create_by?></div>
        </div>
             <?php
             
if($reply_create_by == $create_by) {
 
                $color 'reply_comment';
 
            }else{
 
                 $color='reply_comment_receive';
 
            }
 
         ?>
        <div id="<?php echo $color?>">
            <?php echo $reply_comment?>

            <?php
            if
(!empty($reply_file)){
 
               echo '<br/><br/>';
 
              echo 'فایل های ضمیمه شده: ' "<a target='_blank' href=".base_url()."file_ticket/$reply_file>" .$reply_file '</a>';
 
           }
 
           ?>
        </div>
         <?php ?>
     </div>


<section>
 <script>
$(document).ready(function(){
    $('#reply_ticket').hide();
    $("#btn-reply1").click(function(){
        
        $("#reply_ticket").slideToggle('slow');
    });
});
</script>
<script>
     CKEDITOR.replace( 'comment' , {
         contentsLangDirection: 'rtl'
         config.removePlugins= 'toolbar'
     });
</script> 
Reply
#6

(This post was last modified: 05-16-2017, 12:35 AM by neuron.)

if u used my posted code, there is no reply() method in my code, so you have to set your form action in to

PHP Code:
echo form_open_multipart('ticket/show' $groupsId ); 
//pass $groupsId  from controller to view 
 
after fixing it, when u post form with empty inputs you should see form validation errors
Reply
#7

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/ticket.php

Line Number: 115

Backtrace:

File: C:\wamp\www\ticket\application\controllers\ticket.php
Line: 115
Function: _error_handler

File: C:\wamp\www\ticket\index.php
Line: 315
Function: require_once
Reply




Theme © iAndrew 2016 - Forum software by © MyBB