# Passing Ajax variables to PHP inside a loop

**URL:** <https://discourse.cakephp.org/t/passing-ajax-variables-to-php-inside-a-loop/2704>\
**Category:** Need Help\
**Created:** [June 12, 2017, 2:00pm UTC](https://discourse.cakephp.org/t/passing-ajax-variables-to-php-inside-a-loop/2704 "2017-06-12T14:00:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![angelorigo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/angelorigo/32/193_2.png) [@angelorigo](https://discourse.cakephp.org/u/angelorigo)\
**Post date:** [June 12, 2017, 2:00pm UTC](https://discourse.cakephp.org/t/passing-ajax-variables-to-php-inside-a-loop/2704/1 "2017-06-12T14:00:28Z")

</div>

I am using CakePHP 2 and need to populate two PHP fields with JSON variables from a loop.

How can it be done ?

The variables are the car manufacturer id and the model id.

I have the JSON variables in this format available at JSON Form data:

```
array_brand[0][id]:"4232"
array_model[0][model_id]:"3647"
array_brand[1][id]:"3436"
array_model[1][model_id]:"1623"

```

The loop:

```
<div id="divCars">
    <?php
    for($i = 0; $i <= $arrayCars; $i++)
    {
        include 'ajax_cars.ctp';
    }
    ?>
</div>

```

And inside the loop at ajax\_cars.ctp i would like to populate the two fields:

- Car manufacturer id ( will be : $dataCars[‘id’] )
- And model id (will be $dataCars[‘model\_id’]):

How can i populate $dataCars[‘id’] and $dataCars[‘model\_id’] PHP variables from the available JSON variables :

```
array_brand[0][id]:"4232"
array_model[0][model_id]:"3647" 

echo $this->Html->rows(
         array(
               array(
                     $this->Html->link('Publish Cars', array(
                                                'controller' => 'Cars',
                                                'action' => 'publish',
                                                base64_encode($dataCars['id'])
                                          base64_encode($dataCars['model_id'])
                                )
                            )
                        )
                   ),
            );
```
