1

I have a view which has 2 forms:

<table>
<th>Write a comment.</th>
<tr>
    <td>
        <?php echo form_open($this->uri->uri_string(),$form1); 
              echo form_textarea($comment);
              echo form_submit('submit','submit');
              echo form_close();
        ?>
    </td>
</tr> 


</table>

<table>
    <tr>

        <td>
            <?php echo form_open($this->uri->uri_string()); 
                  echo form_dropdown('portion', $portion_options); 
                  echo form_submit('book','book');
                  echo form_close();
            ?>
        </td>
    </tr>
</table>

In the controller I check which button was clicked and then I perform some action by adding the corresponding form's values to the database.

if(isset($_POST['book']))
{
    //sending the data to the database
    echo "Book button clicked";
}

if(isset($_POST['submit']))
{
   //sending the data to the database
   echo "Submit button clicked";
}

However when the 'book' button is clicked no action is performed. It is like the button was never clicked. Whereas when I click the 'submit' button, every action is done properly.

In the past I have used the same technique on plain php (i mean no framework, just php), and has worked fine for me. Does codeigniter need any further configuration? Am I doing something wrong?

5
  • And oh, tell me array in $form1. Where's it? Commented Sep 13, 2012 at 14:48
  • You should be able to have 2 forms on a page just fine. Can you post the uri_string that you're hitting in your browser and then post your entire method? Commented Sep 13, 2012 at 14:54
  • Can you also post the generated html for the forms? Commented Sep 13, 2012 at 14:54
  • I do have it on the top of my view. This is the array: $form1 = array( 'name' => 'form1', 'id' => 'form1' ); Commented Sep 13, 2012 at 14:54
  • Sorry for my delay. I was looking whether the problem is on the dropdown, and maybe it is there, since I changed the input from a dropdown to a text input and now the clicked button is beeing recognized Commented Sep 13, 2012 at 15:04

3 Answers 3

1

Why not add a hidden field to both forms called form_idwith values 1 and 2 respectively? Easy to catch in your controller upon post; e.g.:

if($this->input->post()){
  switch($this->input->post('form_id')){
  case 1:
    // do stuff
  break;
  case 2:
    // do stuff
  break;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply. I just tried it your way, however, still just the 'submit' button is recognized
Are you using if($this->input->post('book')) {or raw if(isset($_POST['book'])) { ?
Actually I used the $this->input->post() since I thought that from the swich is going to be understood which form was posted. I tried now with $this->input->post('book') and still nothing.
I just tested locally, having echo form_hidden('form_id', 1); and echo form_hidden('form_id', 2); on both forms respectively. When in the controller I do if($this->input->post()){ echo $this->input->post('form_id'); } I get correct output - so it should work.
I think I figured it out. You're checking for if(isset($_POST['submit'])) Since both buttons are submit buttons, your second ifwill always evaluate as true. So you need to check not for the existence of a submit but for the existence of another element, the hidden field as I suggested. Or, you could do this: if($_POST['submit'] == 'submit'){ and if($_POST['submit'] == 'book'){
|
0
<?php echo form_open($this->uri->uri_string(),$form1); 

and

<?php echo form_open($this->uri->uri_string()); 

Looks like you forgot to provide the settings in the second one like:

<?php echo form_open($this->uri->uri_string(),$form2); 

6 Comments

no, either with <?php echo form_open($this->uri->uri_string()); or <?php echo form_open($this->uri->uri_string(),$form2); does not work :/ $form1 = array( 'name' => 'form1', 'id' => 'form1' ); and $form2 = array( 'name' => 'form2', 'id' => 'form2' );
When I unite the forms to 1, then which button was clicked is recognized and work properly. However I need them to be on seperate forms if this is possible.
In any case do not use if(isset($_POST['book'])) in CodeIgniter. Use built in $this->input->post('book');
And see in firebug/chrome developer tools what data is being sent to the controller, it can be seen in Network tab.
Even though I do not know how to use the firebug, in the Headers Tab at the Network tab I see that what I selected from the dropdown has been sent
|
0

Well after spending my whole day on this I finaly managed to solve it somehow (although I believe it is not such a propper way to handle this).

Well:

$comment = array(
    'name'      => 'comment',
    'id'        => 'comment',
    'value'     => 'write you comment',
    'row'       => '5',
    'cols'      => '100'
    );

<table>
<th>Write a comment.</th>
<tr>
    <td>
        <?php echo form_open($this->uri->uri_string()); 
              echo form_hidden('form_id', 1);
              echo form_textarea($comment);
              echo form_submit('submit','submit');
              echo form_close();
        ?>
    </td>
</tr> 


</table> 

<table>
    <th>Write a comment.</th>
    <tr>
        <td>

            <?php echo form_open($this->uri->uri_string()); 
                  echo form_hidden('form_id', 2);
                  echo form_dropdown('comment', $portion_options);
                  echo form_submit('book','book');
                  echo form_close();
            ?>

        </td>
    </tr> 


</table> 

Probably the forms fields (textarea and dropdown) needed to have the same name (which i have set to 'comment'). Although I do not understand why :/

Thank you all for trying to help me :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.