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

[eluser]rabidmacine9[/eluser]
hello everybody I'm trying to make all fields in a form required but I haven't really understand how form_validation works...
so at this point the form doesn't do anything it doesn't post any comments and it doesn't display any errors even when all fields are filed
this is the code of the controller:

Code:
<?php

class MyBlog extends Controller{
        
        
   function MyBlog(){
       parent::Controller();
       $this->load->helper(array('url','form','html')); //here we load some classes that we use
      
       $this->load->scaffolding('entries');  //scaffolfing is a feature that lets you add or remove elements from the database
        $this->load->scaffolding('comments');
        
       $this->load->library('form_validation');//load validation class used to validate our forms...
   }
  
  
      
function comments(){
    
      $data['title'] = "My comment Title"; //the tille on the comments page
      $data['heading'] = "My comment Heading"; //the header on the comments page
      $entry_id = $data ['entry_id'] = $this->uri->segment(3); //here we grub the id of the entry we comment
      $this->db->where('entry_id', $this->uri->segment(3));  //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->query("SELECT * FROM entries WHERE id = $entry_id");  //and this query comes up with the relevant to the comments entry
      
      $this->load->view('comment_view', $data); //loads all data variables to comment_view.php
      
        //here is the validation of the form, all fields required!
        $this->form_validation->set_rules('body', 'Body', 'required');
        $this->form_validation->set_rules('author', 'Author', 'required');
    
        
        
        
      
      if ($this->form_validation->run() == TRUE)
        {    
        
           $this->db->insert('comments', $_POST);
      
           redirect('myBlog/comments/'.$entry_id);
          // $this->comment_insert();
            $this->load->view('myBlog/comments/'.$entry_id);
        }
    
    
    }
      
function comment_insert(){
      
       $this->db->insert('comments', $_POST);
      
       redirect('myBlog/comments/'.$_POST['entry_id']);
       }
      
              


      
      
}

?>

and here is the comment_view.php file with the form...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;



&lt;?php $relevantEntry = $relevantEntryQuery->row_array();?&gt;

<div class='curvebox'>
<h1><div class="title">&lt;?php echo $relevantEntry['title']; ?&gt;</div></h1>
<p><div class="bodyText">  &lt;?php echo $relevantEntry['body']; ?&gt;</div></p>
<p><div class="author">&lt;?php echo $relevantEntry['author']." | ".date("D d M Y h:i:s A", strtotime($relevantEntry['date_time'])); ?&gt;</div></p>
</div>




&lt;?php if($query->num_rows() > 0): ?&gt;
     &lt;?php foreach($query->result() as $row): ?&gt;


<div class='commentBox'>
     <div class="commentauthor">&lt;?php echo $row->author." | ".date("D d M Y h:i:s A", strtotime($row->date_time));?&gt;</div>
    
     <p><div class="commentbody">&lt;?=$row->body?&gt;</div></p>
</div>    

    

    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;






//this is the place where the form is filed!

&lt;? echo form_hidden('entry_id',$this->uri->segment(3));?&gt;

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



<a href="/myBlog/CodeIgniter_1.7.2/index.php/myBlog"  id="linkBack">Back to Blog</a>




&lt;/body&gt;
&lt;/html&gt;

any ideas?
sorry for the size of the question...


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