Using Two Forms In Same Page

Hello,

I have a view \game\game.php with a controller GameController.php and model GameForm.php.

In my view I have one form that, when submitted, is giving another form in the same page. But when submitting the last one, I am redirected to the appliction home-page (server/app/index.php) instead of the game- page (server/app/game).

Any idea why?

This is the code of my view:


             

     <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

     

      <ul>

   <?php 

     for ($i=1; $i<=50; $i++)

     {  

       echo "<li><input type='submit' name='FlagID' value='Flag ".$i."'>";          

     }

     ?>             

       </ul>

     </form>

    </div>

   </td>

    <?php if($_POST['FlagID'])


         $id= $_POST['FlagID'];


         echo( "

       

         <td style='vertical-align: top'>

              

          <form method='post' action='".htmlspecialchars($_SERVER['PHP_SELF'])."'>

           <div class='row'> 

            <div class='column'>". $id."</div>

            <div class='column'><input type='hidden' name='id' value='". $id."'></div>

            <div class='column'> <input type='text' name='value' size='50'></div>

           </div>  

           <div class='row'> <input type='submit' name='check' value='Check'>

           </div>

           </form>

         </td> "); ?> 



I also tried to use a different action for the second form (action=‘check.php’) and therefore created a method actionCheck in my GameController and a view views/game/check.php, but this seems not to be enough: “unable to find the requested action “check.php”.”

What am I missing?

Cheers,

Mattima

What is this? It looks like a cut & paste job. I have never seen $_SERVER[‘PHP_SELF’] in ANY example for Yii.

You really need 50 Submit buttons?

Your code indicates that you have TWO forms both posting to the same action with no indication which form triggered the posting.

What you are missing is that the form action is being set to check.php through $_SERVER[‘PHP_SELF’], not not “index.php?r=game/check”. (check the PHP docs for what this returns)

Look at the contact form in the Yii Skeleton app. Use the activeForm. set the action to array(‘game/check’). I also noticed a number of $variables where are they coming from.

Hi Jkofsky,

thanks for you reply.

I did something with PHP before I met Yii. Copy ;) & paste, probably from W3schools.

As the first submit must be handled by the current view-script.

I could use a form with just two input text-fields flagID and flagValue, but instead of user entering flagId himself, I want him to click the corresponding Flag-button, which will generate a the form with input field for FlagValue (must retain flagID, so post it again in hidden input; NB: both fields come from a static table Flags, no CRUD actions and so on).

Maybe I should use Component instead of submit-buttons.

Thank you for opening my eyes. Works with action=‘check’.

Do you mean the iterator (submit-buttons) and flagID (variable input-value/ activeLabel)?

I guess I was seeing things. It was late and I had been answering alot of posts. Maybe I saw the ‘variables’ in another post…Sorry.