submit multiple inputs within a forloop in codeigniter |
My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is : public function get_questions(){ $this->db->select('id,user_name, question, date'); $this->db->order_by("id", "desc"); $query=$this->db->get('t_questions'); return $query->result(); } My view is: foreach ($questions as $id => $row) : ?> <?php echo "<h5>".$row->question; echo "<br>"; echo "from : ".$row->user_name."</h5>"; echo date('Y-m-d H:i'); echo "<br>"; $q_no='save'.$row->id; $ans_no='answer'.$row->id; echo "<h4> Answer:</h4>"; echo form_open('control_panel'); ?> <textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea> <input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' /> <input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' /> <?php echo form_error($ans_no); echo " <div class='form-group'> <div > <label class='checkbox-inline'> <input type='checkbox' name='add_faq' value='yes' /> Adding to FAQ page . </label> </div> </div> <p>"; ?> <input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/> <?php echo 'answer'.$row->id; ?> <hr> <?php endforeach; ?> and my controller is : $this->load->model('control_panel'); $data['questions']=$this->control_panel->get_questions(); $data['no_of_questions']=count($data['questions']); if($this->input->post($q_no)) { $this->form_validation->set_rules($ans_no,'Answer','required|xss_clean'); if($this->form_validation->run()) { /* code to insert answer in database */ } } of course it did not work with me : i get errors : Severity: Notice Message: Undefined variable: q_no i do not know how to fix it I am using codeigniter as i said in the headline. to explain exactly what i want to do : it is supposed for users to ask the admin , so their questions are saved t_questions table in the database , then i got them in the view by a foreach loop and i wrote a code under each fetched question for a textbox to let the admin answer each question do to validate each answer through the validation rules i differ each answer by the id of the question which is a column in the table t_question and also i am doing the same for the submit button to differ which submit button is clicked that is it |
Welcome Guest, Not a member yet? Register Sign In |