CodeIgniter Forums
Form radio buttons will not validate, need help. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form radio buttons will not validate, need help. (/showthread.php?tid=51568)

Pages: 1 2


Form radio buttons will not validate, need help. - El Forum - 05-09-2012

[eluser]Judgestar[/eluser]
Hi, I am new to codeigniter and php coding in general. I have been trying to get a questionnaire app I am working on to validate radio button selections. I make sure no radio options are selected and hit submit and it just goes to the next page. I do not get any errors or messages telling me 'hey that shouldn't have worked'.

Here is my form code:

Code:
<?php echo form_open('factors_survey/questions/question2');
  
  $factor5 = array(
   'name'   =>   'factor1',
   'id'     =>   'factor5',
   'value'  =>   '5'
  );

  $factor4 = array(
   'name'   =>   'factor1',
   'id'     =>   'factor4',
   'value'  =>   '4'
  );

  $factor3 = array(
   'name'   =>   'factor1',
   'id'     =>   'factor3',
   'value'  =>   '3'
  );

  $factor2 = array(
   'name'   =>   'factor1',
   'id'     =>   'factor2',
   'value'  =>   '2'
  );

  $factor1 = array(
   'name'   =>   'factor1',
   'id'     =>   'factor1',
   'value'  =>   '1'
  );

?>
  <tr>
   <td>
    <table class="Questions" cellspacing="0" width"100%">
     <tbody>
      <tr class="FactorTableHeader">
       <td width="30%">
        <td id="ChoiceFiveHeader" valign="top" class="AnswerText" width="75">Very Important</td>
        <td id="ChoiceFourHeader" valign="top" class="AnswerText" width="75">/td>
        <td id="ChoiceThreeHeader" valign="top" class="AnswerText" width="75">Somewhat Important</td>
        <td id="ChoiceTwoHeader" valign="top" class="AnswerText" width="75">/td>
        <td id="ChoiceOneHeader" valign="top" class="AnswerText" width="75">Not Important</td>
       </td>
      </tr>
       <tr class="FactorOne">
        <td>An interesting job:</td>
        <td valign="top" align="center" class="AnswerRadio" width="75">&lt;?php echo form_radio($factor5); ?&gt;</td>&lt;?php echo form_error('factor5'); ?&gt;
        <td valign="top" align="center" class="AnswerRadio" width="75">&lt;?php echo form_radio($factor4); ?&gt;</td>&lt;?php echo form_error('factor4'); ?&gt;
        <td valign="top" align="center" class="AnswerRadio" width="75">&lt;?php echo form_radio($factor3); ?&gt;</td>&lt;?php echo form_error('factor3'); ?&gt;
        <td valign="top" align="center" class="AnswerRadio" width="75">&lt;?php echo form_radio($factor2); ?&gt;</td>&lt;?php echo form_error('factor2'); ?&gt;
        <td valign="top" align="center" class="AnswerRadio" width="75">&lt;?php echo form_radio($factor1); ?&gt;</td>&lt;?php echo form_error('factor1'); ?&gt;
       </tr>
       <tr class="FactorScale">
        <td width="30%">
         <td valign="top" align="center" class="AnswerText" width="75">5</td>
         <td valign="top" align="center" class="AnswerText" width="75">4</td>
         <td valign="top" align="center" class="AnswerText" width="75">3</td>
         <td valign="top" align="center" class="AnswerText" width="75">2</td>
         <td valign="top" align="center" class="AnswerText" width="75">1</td>
        </td>
       </tr>
       <tr class="FactorSubmit">
        <td width="30%">
         <td valign="top" align="center" class="AnswerText" width="75"></td>
         <td valign="top" align="center" class="AnswerText" width="75"></td>
         <td valign="top" align="center" class="AnswerText" width="75">&lt;?php echo form_submit('submit', 'Next Factor'); ?&gt;</td>
         <td valign="top" align="center" class="AnswerText" width="75"></td>
         <td valign="top" align="center" class="AnswerText" width="75"></td>
        </td>
       </tr>
     </tbody>
    </table>
   </td>
  </tr>
&lt;?php echo form_close(); ?&gt;


and here is my controller code:

Code:
public function FactorQuestions()
{
  $this->load->library('form_validation');

  $this->form_validation->set_error_delimiters('', '<br>');
  $this->form_validation->set_rules('factor1', 'Factor One Selection', 'trim|required|xss_clean');

  if ($this->form_validation->run() === FALSE)
  {
   $data['title'] = "Factors Questionnaire";
   $this->load->view('factors_survey/questions_view', $data);  
  }
  else
  {
   $factor1 = $this->input->post('factor1');
                        
                        //sends radio value to database, work in progress
                        //$factor1score = $this->User_Model->update_factor_score($factor1);

   if (!isset($factor1score))
   {
    //error message
   }
   else
   {
    $survey_data = array('Factor1score' => $factor1);
    $this->session->set_userdata($survey_data);
    redirect('factors_survey/questions/question2');
   }
  }
}

I have a login form for the same app before this part that is working perfectly, it takes the name input form, validates it and sends the post data to my database. It remembers the post value and takes the user to the first question and then the validation for the radio form does not work. I have been working on this for a few days now and I have no idea how to fix this, any help and/or assistance is greatly appreciated.


Form radio buttons will not validate, need help. - El Forum - 05-09-2012

[eluser]CroNiX[/eluser]
Your form is sending to 'factors_survey/questions/question2', but your processing function is FactorQuestions. I also don't see where in your view that you are repopulating the form values that were submitted or displaying any validation errors.


Form radio buttons will not validate, need help. - El Forum - 05-09-2012

[eluser]Judgestar[/eluser]
Thank you for the reply. I did not realize I was sending the form_open to the wrong controller. I made that one change and now I am able to see the validation rules are working Smile Now I am seeing where the rest of my code needs fixing.

As far as repopulating the values and adding validation error code, I have not gotten to that part yet. I will be continuing now with the code because the validation issue was halting my progress. Thank you so much for the tips. I will post an update once I get it fixed and/or if I need additional assistance.






Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]Judgestar[/eluser]
Got most of my app working now thanks to the tip Cronix provided. I am running into another problem and hoping I could get some insight as to what I am doing wrong.

Each question for my app posts and insert the value of that question into the database (12 total), I setup a function to sum all of those scores into another row but it is not inserting the sum'ed data. here is my model code for the sum function

Code:
public function count_hygiene_factor($name)
{
  //query to sum up the scores
  $sql = "SELECT SUM(Factor2Score + Factor5Score + Factor6Score + Factor8Score + Factor9Score + Factor12Score)
      from FactorSurveyScore
      where Name = ?";
  
  $this->db->query($sql, $name);
}

and I have another function to update that score

Code:
public function update_hygiene_factor($HygieneCount, $name)
{
  //query to updte hygiene factor score
  $query = "UPDATE FactorSurveyScore Set HygieneFactorScore = (?) WHERE Name = (?)";

  $this->db->query($query, array($HygieneCount, $name));

  if ($query->num_rows() > 0)
  {
      return $query->result();
  }
  else
  {
   return FALSE;
  }


This is what I have in my controller (probably where most of my trouble is coming from)

Code:
public function survey_results()
{
  $name = $this->session->userdata('name');
  $HygieneCount = $this->User_Model->count_hygiene_factor($name);

  
  $this->load->view('factors_survey/survey_results', $HygieneCount);
}


and my view shows this in an html/php page

Code:
<p>Your Hygiene Factor Score is: &lt;?php echo $HygieneCount; ?&gt;<p>


when I run a test run all it shows is nothing/blank area where a sum'ed number should be. Hoping someone could help me out or point me in the right direction to fix this, and yes I am using the user guide to try to fix this but still not seeing where I am going wrong. Thanks for the help in advance Smile


Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]CroNiX[/eluser]
Well, you're not returning the total from your function
Code:
public function count_hygiene_factor($name)
{
  //query to sum up the scores
  //Need to provide an alias for your total in order to retrieve it from the result
  $sql = "SELECT SUM(Factor2Score + Factor5Score + Factor6Score + Factor8Score + Factor9Score + Factor12Score) AS `total`
      from FactorSurveyScore
      where Name = ?";
  
  //2nd parameter for bindings should be an array of data in order that they appear in the query.
  $q = $this->db->query($sql, array($name))->row();

  if ($q->num_rows() > 0)  //check for result
  {
    return $q->total;  //actually return the value
  }
  else
  {
    return 0;
  }
}



Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]Judgestar[/eluser]
That definately makes sense Smile I had it in my sql query when i was running my initial tests on the data, but did not know I also needed to added it to the sql I put into codeigniter.

I did this but now I am getting an error code after adding the edits to the code.

"Fatal error: Call to undefined method stdClass::num_rows()"

I thought num_rows() function was already defined in codeigniter, how would I fix this?


Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]CroNiX[/eluser]
Try removing the ->row() from the query. I put it there because I'm used to using Active Record, but it looks like it's not needed for a straight db::query() and could be causing the problem.


Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]Judgestar[/eluser]
Yup that was the problem, to display the data im looking for I do this:

Code:
&lt;?php echo $total; ?&gt;

By the way I really appreciate your help Cronix, as I said I am new to this and your tips have really helped me out a ton!


Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]CroNiX[/eluser]
Glad to help.


Form radio buttons will not validate, need help. - El Forum - 05-10-2012

[eluser]Judgestar[/eluser]
hehe, so was that php the correct way to show the data on the html page?