Welcome Guest, Not a member yet? Register   Sign In
Single html Form With Multiple Submit Buttons
#14

[eluser]Unknown[/eluser]
I just started trying to learn CodeIgniter a couple of days ago, and needed some info on how to use two, or more submit buttons, and that's how I found this thread.

( 1 ) This shows the first approach to solving the problem. Obviously none of this is my original work, it is based on this thread and its contributors. I just tested in a sample project and now display the two approaches. It is all debugging code. Using this approach the button names are different, and the button values are not used, this is a key point.

So, it is about showing two different ways of getting the two buttons to do something different.

Code:
<!-- approach ( 1 ) -->

<form method='post' action="">
    <input type="submit" name="create" value="create_button" />
    <input type="submit" name="preview" value="preview_button" />
</form>

<?php
    // +---------+---------+---------+---------+---------+---------+
    // [8500] now these two lines work OK.
    // The button names are different
    // +---------+---------+---------+---------+---------+---------+
    echo ($this->input->post('create'))  
              ? '<br>[8501] submitted using : ' . $this->input->post('create')
              : FALSE;

    echo ($this->input->post('preview'))
              ? '<br>[8502] submitted using : ' . $this->input->post('preview')
              : FALSE;
    ?&gt;

( 2 ) I had looked at the code and comments described by other users on this thread, and tried to make sure their code worked for me. The first time around I could not make the first approach [8500] above work until I realized that the names had to be different. For the second approach [8400], and [8600], the button names are the same, and the button values are used to differentiate which button was clicked, this is also a key point.

Code:
&lt;!-- approach ( 2 ) --&gt;

&lt;form method='post' action=""&gt;
    &lt;input type="submit" name="button_action" value="create_button" /&gt;
    &lt;input type="submit" name="button_action" value="preview_button" /&gt;
&lt;/form&gt;

&lt;?php
    // +---------+---------+---------+---------+---------+---------+
    // [8400] this block works correctly
    // The button names are the same, and the switch statement is
    // based on the value
    // +---------+---------+---------+---------+---------+---------+
    $button_action = $this->input->post("button_action");
    switch($button_action)
    {
        case 'create_button':
            // $this->create();
            echo '<br>[8401] create_button';
            break;
        case 'preview_button':
            // $this->preview();
            echo '<br>[8402] preview_button';
            break;
    }

    // +---------+---------+---------+---------+---------+---------+
    // [8600] these two blocks work correctly
    // This basically does the same as the switch statement above.
    // +---------+---------+---------+---------+---------+---------+
    if ($this->input->post('button_action') == 'create_button')
    {
        // $this->create();
        echo '<br>[8601] create_button';
    }

    if ($this->input->post('button_action') == 'preview_button')
    {
        // $this->preview();
        echo '<br>[8602] preview_button';
    }
?&gt;


Messages In This Thread
Single html Form With Multiple Submit Buttons - by El Forum - 12-10-2010, 03:34 PM
Single html Form With Multiple Submit Buttons - by El Forum - 12-10-2010, 04:30 PM
Single html Form With Multiple Submit Buttons - by El Forum - 12-11-2010, 03:36 AM
Single html Form With Multiple Submit Buttons - by El Forum - 12-11-2010, 04:07 AM
Single html Form With Multiple Submit Buttons - by El Forum - 12-11-2010, 06:55 AM
Single html Form With Multiple Submit Buttons - by El Forum - 12-11-2010, 08:40 AM
Single html Form With Multiple Submit Buttons - by El Forum - 12-12-2010, 12:45 AM
Single html Form With Multiple Submit Buttons - by El Forum - 12-12-2010, 12:45 AM
Single html Form With Multiple Submit Buttons - by El Forum - 08-04-2012, 09:08 PM
Single html Form With Multiple Submit Buttons - by El Forum - 08-08-2014, 06:39 PM
Single html Form With Multiple Submit Buttons - by El Forum - 08-08-2014, 09:02 PM
Single html Form With Multiple Submit Buttons - by El Forum - 08-09-2014, 12:18 PM
Single html Form With Multiple Submit Buttons - by El Forum - 08-09-2014, 01:19 PM
Single html Form With Multiple Submit Buttons - by El Forum - 08-09-2014, 07:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB