Welcome Guest, Not a member yet? Register   Sign In
Small form validation issue
#1

[eluser]kaos78414[/eluser]
So I'm still a novice with codeigniter but slowly but surely I am learning. (Next I am going to try to implement AJAX).

So I have this comment form, and it shows up correctly and the comments show up correctly and the blog post shows up correctly. It is also written to display "No Comments" when there are none, and that works. So everything looks right and the url looks like root/blog/view/(blog_id)

But when submitting a comment that doesn't pass validation it goes to blog/comment_submit, so $this->uri->segment(3) doesn't find the blog id, and can't pass it to the hidden form so trying to submit a comment from there only brings up a blank page.

My code looks like this-

Controller
Code:
function comment_submit() {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('author', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('body', 'Comment', 'required|min_length[10]');
        
        if ($this->form_validation->run() == FALSE) {
            if (isset($_POST['blogid'])){
                $this->view($blog_id = $_POST['blogid']);
            }
            else {
                $data['body'] = 'error_404';
                $data['subtitle'] = '404 - Error';
                
                $this->load->view('includes/template', $data);
            }
        }
        else {
            $this->commentmodel->insert_comment();
            
            $data['message'] = 'Comment successfully posted.';
            
            redirect('blog/view/'.$_POST['blogid']);
        }
    }

The other problem is that the message value won't pass using redirect. (So I can't give a success message)

Do I absolutely need to use AJAX for this or is there another way?

Can anyone see anything else I may be doing wrong?
#2

[eluser]FrancoNoronha[/eluser]
on condeigniter's video tutorial page there is that video showing exactly what you need... is a comment page / form build... take a look see if it helps... I am new at this so a can't help you more.. : )
#3

[eluser]kaos78414[/eluser]
Well I think what it does is load a success page in that tutorial, but what I'm trying to do is take the user back to the same page and pass a message using something like

if (isset($message) {
echo $message;
}

The problem is the url doesn't allow me to get the blogid... maybe if I use sessions?
#4

[eluser]FrancoNoronha[/eluser]
Have you trt using $this->uri->segment()? You can pass a code like somepage/1 or somepage/99 and get the uri with this function...
#5

[eluser]kaos78414[/eluser]
No, that's what I was saying. If a comment doesn't go through correctly, it displays validation errors and everything, but the page url is blog/comment_submit (thus rendering $this->uri->segment useless in this case)

If I use redirect instead of calling the view function it won't display validation messages.

On the other hand, when a comment successfully passes validation, it is redirected back to the view function, but the success message is not displayed. (I'm not sure why)
#6

[eluser]FrancoNoronha[/eluser]
I'm working on a form too... and i i'm looking over some pages i found ... maybe this page can help you...Redirect with data...

they suggest the use of CI session... if i come by any better idea, i'll let you know!
#7

[eluser]kaos78414[/eluser]
I'll give that a shot! Seems like exactly what I need. I was wondering if I should just use session data. (although for this particular projects session data seems like overkill, if there is a better way I'd like to do that.)
#8

[eluser]kaos78414[/eluser]
Yup, that worked just fine. Now I just need to clean up some of the code. Big Grin
#9

[eluser]FrancoNoronha[/eluser]
Great to hear that!!!




Theme © iAndrew 2016 - Forum software by © MyBB