Collecting Tabular Input->AJAX

Hi there

I want to use tabular data collection but I am facing some problems with it. see as you have to use


<?php echo CHtml::activeTextField($item,"[$i]name"); ?> 

with


key in above it is $i 

\ this will eventually help traversing through all records while we are in controller.

Now what the problem is ?

I am trying to implement via


AJAX

. Now i need some sort of variable to keep track of index.That is what I am stuck with.

other wise my form gets fields added when I click add and they are just fine. How can you manage the index?

following is my complete code

Controller




public function actionAddHotelPictures($id,$key)

        {

            $model = new HotelImages;

            $this->render('hotelPicture',array(

                    'model'=>$model,

                    'key'=>$key,

            ));

        }

public function actionAddImageRow()

        {               

            $key = $_GET['key'];

            

            $this->renderPartial('_singleImageRow', array('key'=>$key, 'model'=>new HotelImages),false,true);

            

        }

View


<?php


echo CHtml::ajaxLink(Yii::app()->createUrl('/AddImageRow'),

        array('hotel/AddImageRow','key'=>++$key),

        array(

            'type' => 'GET',

            'update'=>'#asd',            

             'success' => "function( data )

                 {

                 alert(data)

                 //$('table tbody').append(data); 

                  }",

            'error'=>"function( xhr )

                 {

                    alert(this.url)

                  }",

            'data'=> array( 'key'=> ++$key )

            )

        );

            

            ?>

_singleImageRow




<tr>    

    

    <td>Image Title <?php echo $key;?></td>

    <td><?php echo CHtml::activeTextField($model,"[$key]title"); ?></td>

    <td>Image</td>

    <td><?php echo CHtml::fileField('Immagini[immagine]'); ?></td>

</tr>



Even I am incrementing $key in Ajax Function but I donot know why it still every time passes same value that is one increment to value that was orignally given from actionAddHotelPictures

for ex if 2 was passed as key then every time 3 is passed in ajax to actionAddImageRow no matter how many time you click it

Hello,

++$key => this is basically passed from the controller, if the initial value as you said is 2 then it will remain 2. As you know PHP is server side so ++$key on the ajax side will only increment the initial value that was passed, so in your case (2) it will be 2+1.

I suggest pass the value from client side/javascript.

Or maybe keep a static variable inside actionAddImageRow() and see if that will work for your purpose. Example:

static $key = $val + 1;

Thanks.

cant use static variable gives error and how can I send variable from my Ajax to controller any examples? I mean lets say if you decelare variable in ajax then on every click it will be re declared and increment will not work .

Please can you put an example?

Well, i’m not really sure the context you are working in, however try something like this and see if it works for you:

&#036;(document).ready(function() {


    var count = 0; //This keeps track of the key you may put it like var count = &lt;?php echo &#036;key; ?&gt;


    &#036;('.yourButton').click(function(e) {


        count = count+1;


          &#036;.ajax({


              cache: false,


              // Other ajax stuff


              data: {


                key = count //Here, you pass the key, as you can see on each click it gets incremented


              }


       });