CodeIgniter Forums
One Form, Two Buttons - 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: One Form, Two Buttons (/thread-10476.html)



One Form, Two Buttons - El Forum - 08-01-2008

[eluser]Frychiko[/eluser]
I have a form with two submit buttons, but am having trouble trying to take a different action depending on the button pressed.

Here's a section of the form:

Code:
<p>Are you sure you want to delete this record?</p>

<div class='buttons'>
&lt;?=form_button("delete",'Delete')?&gt;
<span class='space'></span>
&lt;?=form_button("cancel",'Cancel')?&gt;
</div>

Here's the validation class run part:

Code:
if ($this->validation->run() == FALSE)
{
    // prepopulate form
    $this->validation->id = $id;
}
else
{
    if ($this->input->post("cancel"))
        echo "cancel!";
}

There is no "cancel!" output, so there's no cancel post variable..?
If I remove the if statement, it will echo "cancel!"..


One Form, Two Buttons - El Forum - 08-01-2008

[eluser]xwero[/eluser]
The button that is clicked is added to the post global so your code is correct. But you should check the clicked button to perform the validation so your code would have to be something like
Code:
if(isset($_POST['delete']) && ! $this->validation->run())
{

}
elseif(isset($_POST['cancel']))
{

}



One Form, Two Buttons - El Forum - 08-01-2008

[eluser]Frychiko[/eluser]
Thanks xwero!, using the $_POST variable directly works.

I don't understand why I can't use the input->post() in this case but anyway it works now..