CodeIgniter Forums

Full Version: [solved] Form validation and dynamic URLs causing havoc :-(
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]boltsabre[/eluser]
Hi guys,

I have a forum, and the URLs are all dynamic. When a user click 'add comment' to a particular post, the URL is different from another post (can differ in Category and Post Id values).

Heres 2 sample URLs from the forum when user clicks 'add comment':
www.mydomain.com/forum/post-reply/general-topics/3/and-back-again
www.mydomain.com/forum/post-reply/working-holiday/1/and-another-one

uri 1 = controller, uri = function (post-reply), and uri 3-5 are dynamic (category, id, title).

But when the user tries to add a comment to a post and fails in the form validation I'm running into trouble because of this code in the controller:
Code:
// If validation fails, else process user new post
if ($this->form_validation->run() == FALSE){
     $this->load->view('forum-post-reply-view', $this->data);
}else{

Obviously when form validation fails, I'm loosing my 'dynamic' url.

Is there anyway to get around this apart from putting all my dynamic elements in hidden form fields?

Thanks in advance, really hoping someone can help me out here!

El Forum

[eluser]oneos[/eluser]
I believe, if you added parameters into post-reply you could capture the data pretty easily.

For example, http://www.mydomain.com/forum/post-reply...back-again should have:
Code:
<form action="forum/post-reply/general-topics/3/and-back-again" method="POST">
    ...



Code:
function post-reply($cat, $id, $title)
{
    ...

    // If validation fails, else process user new post
    if ( ! $this->form_validation->run())
        $this->load->view('forum-post-reply-view', $this->data);
    else
    {
        // insert to DB here and such
        redirect('forum/post-view/'."$cat/$id/$title", 'location');
    }
}

Or somesuch.

By the end of it, you're either back on post-reply with the dynamic URL in place already, or you're back to looking at the post, once again with your dynamic URL.

Hope this helps!

El Forum

[eluser]boltsabre[/eluser]
cool, thanks for the really fast reply, will check it out on saturday, just about to switch off for the nite and I'm full up tomorrow!!! THANKS!!!

El Forum

[eluser]boltsabre[/eluser]
Ah thank you very much, sorry about the late reply, a combination of being busy, being sick and having my birthday has meant I had to put my beloved project on ice for a week or so.

Anyway, going of your suggestion, the answer turned out to be very very simple.

My old form tag looked like this:
Quote:<?php echo form_open('visas/forum/post-reply/'

Simply by adding the uri segments as such,
Quote:<?php echo form_open('visas/forum/post-reply/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6)); ?>

fixed the whole issue. Now if the form does not validate it redirects back to the correct view with my dynamic url still in place. Now... I just hope that this fixes my random pagination problem that seems to occur every so often when a reply is submitted after failing validation the first time around.