Welcome Guest, Not a member yet? Register   Sign In
One form, many actions
#1

[eluser]Sector[/eluser]
Hello, once again I'm stuck on something, and not being able to find a solution that's not hackerish is nagging at me.

I have a form, and it contains many checkboxes. (one for each item in a table).
At the end of that form, you can press multiple submit buttons. The form is a POST
form that points to the controller action with method delegate. I envisioned this method because I cannot let a form point to multiple URLs.

What I want to happen is: if a person checks a few checkboxes and presses the 'delete' button, I want to delegate page to reroute to action/delete, with exactly the same POST array.

How can I accomplish this with Code Igniter?

Thanks very much in advance!
#2

[eluser]Nillian[/eluser]
Hi there,

I'm a newbie myself so forgive me if I give you wrong information, but here goes anyway.

I assume that your various submit buttons have differing "name" attributes in the HTML?

You should be able to put a small piece of code in your 'delegate' function that decides which of these buttons has been pressed and takes the appropriate action (calling the right function from the related Model/Library)

An example, in semi-pseudo-code...

Code:
function delegate()
{
    if ($this->input->post('deletebutton'))
    {
        // You could simply call the function...
           $params = $this->input->xss_clean($_POST);
           $this->delete($params);
          
        // Or, put the 'delete' method inside a Model or a custom Library
           $params = $this->input->xss_clean($_POST);
           $this->load->model('my_model');
           $this->my_model->params = $params;
           $this->my_model->delete();
    }
    elseif ($this->input->post('editbutton'))
    {
        // Etc...
    }
}

Other than the above code, if you really wanted to do a redirect, that gets a bit more messy I think, what with having to pass the $_POST array on again. You'd probably have to register the contents of $_POST as some kind of global variable to last across pages, and I'm not entirely sure how you do that (yet). I'll browse the forum and see if I can't find a way.

That help at all?

- Nillian
#3

[eluser]Sector[/eluser]
Thanks Nillian, you gave me an idea that I will try!

->

Yes, I had given them different names, so I could branch. Since the pages I wanted to redirect to are in the same file (action controller), I might just as well call the functions:

Code:
function delegate($id) {
   if ($this->input->post(...)) {
      $this->...();
   }
   elseif {
   ...
   }
}

I'll see if it works, thanks Smile
#4

[eluser]Nillian[/eluser]
Glad to be of help Smile
#5

[eluser]alpar[/eluser]
yuu could add a simple javascript function thet would ease your job.

something like

function submit(form,action)
{
form.action = action;
form.submit();
}

and the yiu will have something like <input type="button" onclick="submit(this,'delete')" /> and you can add an action to each of thease buttons one for delete and other for other actions, this is more clear than a server side solution, if you have a single buton, you could also add javascript, i am not an exper and i don't guarantee that the above function works as it, i don't have time to tst it... but somehing simmilar will do
#6

[eluser]Sector[/eluser]
That is also a solution which I would consider as viable, because my webpage is completely Ajax-ified.

But now I got the server-side solution working wonderfully and very elegantly, so I'll stay with that now:

Code:
function delegate() {
        $functions = array(
            'factureren',
            'plaatsen',
            'etiketten'
        );
        
        foreach ($functions as $function) {
            if ($this->input->post($function)) {
                $this->$function();
            }
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB