CodeIgniter Forums
Pass variable in url on form submit - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Pass variable in url on form submit (/thread-41749.html)



Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]Wonder Woman[/eluser]
Hi,

I have an edit url set up so that the article id is appended on the end, however when I got to submit the form it doesn't work because I can't append this variable on the end.

View:
Code:
echo anchor('admin/pages/edit/'.$page->id, 'Edit');

Controller:

function edit($page_id)
Code:
{
    $this->session->set_userdata('page_id', $page_id);
    $data['page_id'] = $this->session->userdata('page_id');
    $this->form_validation->set_rules('content','content','trim|required|htmlspecialchars');
    $this->form_validation->set_error_delimiters('<p class="error">* ','</p>');
    
    if($this->form_validation->run() == FALSE){
        $this->load->view('admin/pages/edit', $data);
    } else {            
        redirect('admin/pages/edit', $data);
    }
}

How can I do it so that the page will submit and go back to 'admin/pages/edit/1', for example.

Many thanks in advance.


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Use the $page_id var and append/concatenate like you did in the anchor function in the redirect()

Code:
redirect('admin/pages/edit' . $page_id, $data);



Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]Wonder Woman[/eluser]
I have tried that but it doesn't work, my browser says there is a problem loading the page.


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Quick notice i guess you're using javascript to submit otherwise an anchor/link will not work to subit a form you need a form_open() and a form_close() thing.

More under
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Sorry i miss the '/'
Now try it again
Code:
redirect('admin/pages/edit/' . $page_id, $data);



Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Quick question do you have index function and are you using that to submit to the edit function in the same controller?


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]Wonder Woman[/eluser]
I have an initial view, pages.php:

Code:
anchor('admin/pages/edit/'.$page->id, 'Edit').' '.anchor($page->url, 'View');
// this is for every record

This then takes you to my edit page:

Code:
echo form_open();
echo form_label('Content: ','content');
echo form_hidden('page_id', $edit->id);
echo form_textarea('content', $edit->content, 'id="content"');
echo form_submit('save', 'Save');
echo form_close();

This is the edit function in the controller:

Code:
function edit($page_id)
{
    $this->session->set_userdata('page_id', $page_id);
    $data['page_id'] = $this->session->userdata('page_id');
    $this->form_validation->set_rules('content','content','trim|required|htmlspecialchars');
    $this->form_validation->set_error_delimiters('<p class="error">* ','</p>');
        
    if($this->form_validation->run() == FALSE){
        redirect('admin/pages/edit/'.$page_id, $data);
    } else {            
        redirect('admin/pages/edit', $data);
    }
}

Hope this makes sense...


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Ok sorry on your redirect you can't pass any data

more info here
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


Pass variable in url on form submit - El Forum - 05-16-2011

[eluser]d1a8lo24[/eluser]
Also if you were using the session to see if the page id was being past you can remove it.

If you were trying to pass that id then you don't need to put it in the array data since you already set in the session just use the session class to output the result in your view.