Welcome Guest, Not a member yet? Register   Sign In
How to keep the url on the same page after form validation?
#1

[eluser]123wesweat[/eluser]
Hi,

Need some fresh pair of eyes

the following updates the database but i don't know how to "stay" on the same url???

1/ I have a form on a dynamic page
2/ page has content from db (form not yet)
3/ form has an action="test/save"
4/ routing is enable like
Code:
//to get the right content from db
$route['test/banksy'] = "testForm/show_content/1";
$route['test/futura'] = "testForm/show_content/2";
//save form data in db
$route['test/save'] = "testForm/test";

this is my testForm -> test function
Code:
function test(){
        $this->load->helper(array('form','url'));
        $this->load->library("form_validation");
        $this->load->model('forms/test_formc_model');
          
        $content_id = $this->input->post('hidden_content_id');
        $options = array ('content_id' => $content_id);
        $data['showContent'] = $this->test_formc_model->get_content($options);
        $data['hiddenContentID'] = $content_id;
        $val = $this->form_validation;

        $val->set_rules('fname', 'fname', 'trim|required');
        $val->set_rules('lname', 'lname', 'trim|required');
        $val->set_rules('item', 'item', 'trim|required');
        $val->set_rules('email', 'email', 'email|required|valid_email');

        if ($val->run() == FALSE) {
            //show errors
            $this->load->view('testfolder/view_test_mainC', $data);
            
        } else {
                //put in our DB
                $res = $this->test_formc_model->insert_data();
                if($res == 1) {
                    $this->load->view('success');
                    return false;
                } else {
                    $msg = 'hmm, db off.';
                }
        }
    }

Here's a quick example
city2go.net/test/banksy
city2go.net/test/futura

Regards,
#2

[eluser]rogierb[/eluser]
There are several ways to go about:

1: change your form action so it stays in the same url and check for count($_POST)
2: use flashdata to store the old url.
3: several other ways, like storing where you came from in a $_POST variable etc
#3

[eluser]123wesweat[/eluser]
Tx rogier
[quote author="rogierb" date="1266609129"]There are several ways to go about:

1: change your form action so it stays in the same url and check for count($_POST)
2: use flashdata to store the old url.
3: several other ways, like storing where you came from in a $_POST variable etc[/quote]

@1, ??? check if something is submitted and then do the validation??? So i must do the count($_POST) in the index function

I do have routing enabled, so test/banksy will always activate the show_content function. And i like to stay on test/banksy.
And i would like to show a thank you message??

@2 will look that up

@3 hope 1 or 2 will work for me Wink
#4

[eluser]jbreitweiser[/eluser]
OK. It seems you are rerouting based on the url. So banksy is content_id 1 and futura is 2. You are translating those names to an ID. What you need to do is get the name back from the ID so you can use it to create your post url. Store the name in the database. So add a field to the content record called url_postback and store 'banksy' for 1 and futura for 2. If validation fails you have to get the content information anyway so write you action='test/$url_postback' for your form tag. So your initial display function needs to be changed as well as you save function. I hope that makes sence.




Theme © iAndrew 2016 - Forum software by © MyBB