Welcome Guest, Not a member yet? Register   Sign In
Two Submit Buttons in Forms
#1

[eluser]Bl4cKWid0w[/eluser]
I have a "form" that requires the user to accept or decline a request. I tried using $_POST with an if statement, but all that did was cause the CI scripts to think that the name of the button was a variable. How would I do something like this?

Example:
<form action='$_SERVER['PHP_SELF']' method='POST'>
<input type='submit' name='accept' value='Accept'>
<input type='submit' name='decline' value='Decline'>
</form>
#2

[eluser]Renard Urbain[/eluser]
This is "only" possible in asp.net.

Otherwise you have to do or:

<form action="<action>" method="post">
<input type="submit" name="accept" value="Accept" />
</form>
<form action="<action>" method="post">
<input type="submit" name="decline" value="Decline" />
</form>

OR:

<form action="$_SERVER[‘PHP_SELF’]" method="POST">
<input type="submit" name="accept" value="Accept">
<input type="submit" name="decline" value="Decline">
<input type="hidden" name="action" id="action" value="" />
</form>
#3

[eluser]Bl4cKWid0w[/eluser]
[quote author="Renard Urbain" date="1226371437"]This is "only" possible in asp.net.
[/quote]

Not true. I've done it with regular PHP before I started using code igniter.
#4

[eluser]Renard Urbain[/eluser]
Interesting, that is something I haven't tried so far.

So what do you want to do exactly ?

If you press accept $_POST = array('accept' => 'Accept'); otherwise $_POST = array('decline' => 'Decline');
#5

[eluser]Bl4cKWid0w[/eluser]
Something like this:

if(!($_POST['accept']) || ($_POST['decline'])){
load form
} else {
perform action
}

Basically if neither of the buttons are pressed, the form is submitted. If one of them is pressed, a function is performed that will determine which one is pressed and will go from there.

Edit: Sorry, if neither of the buttons are pressed, the form is displayed, not submitted.
#6

[eluser]Renard Urbain[/eluser]
Ah!

Not exactly in the best practices but I would do something similar to :

if(!isset($_POST['accept']) or !isset($_POST['decline']))
$this->load->view('the_form');
else
// do the stuff

Note that to access the $_POST you need to set $config['enable_query_strings'] = TRUE; in /system/application/config/config.php otherwise you can access your post arrray via $this->input->post(<name>);
#7

[eluser]BaRzO[/eluser]
in ci you can do this

in controller
Code:
&lt;?php

function show_form()
{
    $this->load->helper('form');
    
    $this->load->view('test_form');
}

function get_vales()
{
    if ( $this->input->post('button1') )
    {
        echo "button one clicked<br>";
    }
    else
    {
        echo "button one not clicked<br>";
    }
    
    if ( $this->input->post('button2') )
    {
        echo "button two clicked<br>";
    }
    else
    {
        echo "button two not clicked<br>";
    }
    
    
}

view file
Code:
&lt;?=form_open("get_values")?&gt;

&lt;input type='submit' name='button1' value='Click' /&gt;
<br>
<button name="button2" value="some_value">Click</button>
&lt;/form&gt;
#8

[eluser]Bl4cKWid0w[/eluser]
Thanks, Barzo. That worked.
#9

[eluser]Nick Husher[/eluser]
Note that you could just name the buttons the same thing with different values:
Code:
//HTML
&lt;input type="submit" value="Save" name="submit_action" /&gt;
&lt;input type="submit" value="Save and Finish" name="submit_action" /&gt;

Code:
//PHP
$action = $this->input->post('submit_action');

if($action == 'Save') {
...
} else if($action == 'Save and Finish') {
...
}
#10

[eluser]dimethroxy[/eluser]
[quote author="Nick Husher" date="1226456839"]Note that you could just name the buttons the same thing with different values:
Code:
//HTML
&lt;input type="submit" value="Save" name="submit_action" /&gt;
&lt;input type="submit" value="Save and Finish" name="submit_action" /&gt;

Code:
//PHP
$action = $this->input->post('submit_action');

if($action == 'Save') {
...
} else if($action == 'Save and Finish') {
...
}
[/quote]

This is actually the best method.




Theme © iAndrew 2016 - Forum software by © MyBB