Afnan Bashir
Afnan Bashir

Reputation: 7419

Collecting Tabular Input with AJAX

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 0

Upvotes: 2

Views: 1956

Answers (1)

Alfredo Castaneda Garcia
Alfredo Castaneda Garcia

Reputation: 1659

Why are you sending $key twice in the ajax link? Here:

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

and here:

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

Maybe it is causing some malfunction which corrupts the values.

Upvotes: 2

Related Questions