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

[eluser]Tim Brownlaw[/eluser]
Not exactly sure what you are trying to show here Bill!

But your Block [8500] won't display any values for the post variables named preview or create because they do not exist in the form!
Code:
// +---------+---------+---------+---------+---------+---------+
// [8500] these two lines do nothing
// +---------+---------+---------+---------+---------+---------+
echo '<br>[8501] do nothing ' . $this->input->post('preview'); // This will return the Boolean FALSE
echo '<br>[8502] do nothing ' . $this->input->post('create');   // This will return the Boolean FALSE

The following is Soley For Debug Purposes to see what is happening!!!
Code:
// +---------+---------+---------+---------+---------+---------+
// [8510] these two lines do nothing as the values do not exist
// +---------+---------+---------+---------+---------+---------+
echo '<br>[8511] I do something but  ';
echo ($this->input->post('preview')===false)?'I Do Not Exist':'I do Exist';
echo '<br>[8512] I do something but ';
echo ($this->input->post('create')===false)?'I Do Not Exist':'I do Exist';
// +---------+---------+---------+---------+---------+---------+
// [8520] Var_dump the Post Values - Both will Return False
// +---------+---------+---------+---------+---------+---------+
echo '<br>[8521] var_dump of preview';
var_dump($this->input->post('preview'));
echo '<br>[8522] var_dump of create';
var_dump ($this->input->post('create'));

Interesting Note: When echoing a string with a concatenated ternary like...
Code:
echo '<br>[8511] I do something but  '. ($this->input->post('preview')===false)?'I Do Not Exist':'I do Exist'
The first string part ie
Code:
'<br>[8511] do something but '.
- does not display. Only the result of the ternary displays. So the fix is to put them as separate echo calls!
#12

[eluser]CroNiX[/eluser]
Code:
$this->input->post('preview')
assumes you have a input with a NAME of preview, not the value.
#13

[eluser]Tim Brownlaw[/eluser]
@CroNiX Smile well that's the short answer!
#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:
&lt;!-- approach ( 1 ) --&gt;

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

&lt;?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;




Theme © iAndrew 2016 - Forum software by © MyBB