CodeIgniter Forums
redirect after post? - 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: redirect after post? (/showthread.php?tid=43006)

Pages: 1 2 3


redirect after post? - El Forum - 06-26-2011

[eluser]Zero-10[/eluser]
My site is setup as such:
domain.com/section/articles/subsection/articleid

naturally the article id is stored as say... $articleid and is called upon for breadcrums and other things but one thing that I can't seem to get is have a form redirect to that specific article.

I tried things like (inside the comment post function)
redirect('section/articles/subsection/' . $articleid);

in function commentpost but it doesn't do too well and the url is always
section/articles/commentpost

What do I do? Sad


redirect after post? - El Forum - 06-26-2011

[eluser]Wondering Coder[/eluser]
why don't you try using route.?


redirect after post? - El Forum - 06-26-2011

[eluser]pmoroom[/eluser]
I don't understand the problem. Are you saying you are not getting $articleid?


redirect after post? - El Forum - 06-26-2011

[eluser]Zero-10[/eluser]
What I'm getting is something like:
mysite.com/section/articles/comment_insert

instead of getting:
mysite.com/section/articles/subsection/articleid

In other words, when [submit] is clicked, it should basically refresh the page but it doesn't. I'd love to have it be AJAX one day but keeping it simple first would be the most ideal.


redirect after post? - El Forum - 06-26-2011

[eluser]cideveloper[/eluser]
post some code. Where is the form action pointing to?


redirect after post? - El Forum - 06-26-2011

[eluser]Zero-10[/eluser]
Form is pointing to
Code:
function comments_insert()
        {
            $this->db->insert('comments', $_POST);
            redirect('section/articles/subsection/' . $articleid);
        }

I simplified the redirect to make it more obvious of what I'm talking about. It's in the same controller as the article is found from.


redirect after post? - El Forum - 06-26-2011

[eluser]aquary[/eluser]
You forgot to put the articleid inside the function's parameter.
Code:
function comments_insert($articleid)
        {
            $this->db->insert('comments', $_POST);
            redirect('section/articles/subsection/' . $articleid);
        }

If the articleid is in $_POST, you'll have to use the $_POST['articleid'] instead, but normally I'd put it in the URI.


redirect after post? - El Forum - 06-26-2011

[eluser]Zero-10[/eluser]
I did that before, does nothing.


redirect after post? - El Forum - 06-26-2011

[eluser]aquary[/eluser]
What was you form's code anyway? the action part is to where? Maybe it'll be more useful if you show us the full code, so we could understand you motive Smile


redirect after post? - El Forum - 06-26-2011

[eluser]skunkbad[/eluser]
Code:
$segs = $this->uri->segment_array();
// assuming that article ID is always the last URI segment
$articleid = end( $segs );
redirect('section/articles/subsection/' . $articleid);

Normally I post all forms to the same method, and by doing that, the above code would work. You'd just have to run form validation and determine if there is a valid post vs a page load of the article.

I think for your code, you might try this:

Code:
function comments_insert()
{
    $this->db->insert('comments', $_POST);
    redirect('section/articles/subsection/' . $this->input->post('articleid'));
}

Although unless you are validating the $_POST array and rewriting $_POST, you're using some potentially unsafe code;