Welcome Guest, Not a member yet? Register   Sign In
trying to understand form_validation
#7

[eluser]pickupman[/eluser]
What's the point of the redirect() and having a load->view() following it. It will never get called. Also, you data may not be inserting if your field names don't match your DB fields. You also need to watch your html structure on your comments_view file. You have elements in the wrong areas possibly causing your problems. You have a hidden form input above the form tag which will do nothing. You had mixed open and closing div's & p's.

Code:
//View
<?php echo validation_errors(); ?>
<?php echo form_open('myBlog/comments/' . $entry_id, array('entry_id' => $this->uri->segment(3)));?>


<div class="theForm">
    <h3>Post Your Comment Here:</h3>
    <div class="in">
        <p>
            <label for="body">Comment:</label>
            &lt;textarea name="body" rows = "10" cols="60" class="txt"&gt;&lt;?php echo set_value('body');?&gt;&lt;/textarea&gt;
        </p>
    </div>
    <div class="in">
        <p>
            <label for="author">Author:</label>
            &lt;input type="text" size="30" name="author" class="txt" value="&lt;?php echo set_value('author');?&gt;" /&gt;
        </p>
    </div>
    <p>
        &lt;input type="submit" value="Submit Comment" class="btn"/&gt;
    </p>
</div>
&lt;/form&gt;

//MyBlog controller
function comments(){
    
      $data['title']   = "My comment Title"; //the tille on the comments page
      $data['heading'] = "My comment Heading"; //the header on the comments page
      $data['entry_id'] = $this->uri->segment(3); //here we grab the id of the entry we comment
      
      $this->db->where('entry_id', $data['entry_id']);  //here we assign the number of the entry to an entry_id number in the comments table
      $data['query'] = $this->db->get('comments'); //this is a query for comments
      $data['relevantEntryQuery'] = $this->db->get_where('entries', array('id' => (int)$data['entry_id']));  //and this query comes up with the relevant to the comments entry
      
      //here is the validation of the form, all fields required!
      $this->form_validation->set_rules('body', 'Body', 'required|trim');
      $this->form_validation->set_rules('author', 'Author', 'required|trim');
      $this->form_validation->set_rules('entry_id', 'entry id', 'required|trim|is_numeric');      
      
      if ($this->form_validation->run() == TRUE)
      {    
            foreach($_POST as $key => $value){
                $this->db->set($key, $value); //make a little safer
            }
            $this->db->insert('comments');
      
           redirect('myBlog/comments/'.$entry_id, 'refresh'); //prevent reload/reinsert        
      }else{
        
        //Form loaded for first time or form failed. Load form
        $this->load->view('comment_view', $data);
      }
    
    }


Messages In This Thread
trying to understand form_validation - by El Forum - 07-02-2010, 01:39 PM
trying to understand form_validation - by El Forum - 07-02-2010, 08:33 PM
trying to understand form_validation - by El Forum - 07-03-2010, 05:38 AM
trying to understand form_validation - by El Forum - 07-03-2010, 06:43 AM
trying to understand form_validation - by El Forum - 07-03-2010, 11:31 AM
trying to understand form_validation - by El Forum - 07-03-2010, 12:28 PM
trying to understand form_validation - by El Forum - 07-03-2010, 04:08 PM
trying to understand form_validation - by El Forum - 07-04-2010, 07:45 AM
trying to understand form_validation - by El Forum - 07-04-2010, 08:05 AM
trying to understand form_validation - by El Forum - 07-04-2010, 08:15 AM
trying to understand form_validation - by El Forum - 07-04-2010, 11:02 AM
trying to understand form_validation - by El Forum - 07-04-2010, 11:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB