[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;
?>
( 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:
<!-- approach ( 2 ) -->
<form method='post' action="">
<input type="submit" name="button_action" value="create_button" />
<input type="submit" name="button_action" value="preview_button" />
</form>
<?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';
}
?>