Форумы / Cotonti / Extensions / Support / Data to TPL with AJAX question

Twiebie
#1 01.10.2012 22:05

I'm trying to get data into a TPL from a PHP file with AJAX.

The code I currently have is as following:

This is the JavaScript bit:

JavaScript
1
2
3
4
5
6
7
8
9
10
$(document).ready(function() {
    function updateTesting(){
        ajaxSend({
            method: "GET",
            url: 'index.php?r=test&a=update',
            data: "",
            divId: 'test_div'
        });
    }
    updateTesting();

And test.ajax.php:

PHP
1
2
3
4
5
6
7
8
9
10
11
if ($a == 'update') {
    foreach ($res->fetchAll() as $row) {
        $t->assign(array(
            'TEST_USERNAME' => cot_build_user($row['test_userid'], htmlspecialchars($row['test_username'])),
            'TEST_MESSAGE' => cot_parse($row['test_message'], $cfg['plugin']['comments']['markup']),
            'TEST_DATE' => cot_date('M jS', $row['test_date']),
            'TEST_DATETIME' => cot_date('G:i', $row['test_date'])
        ));
        $t->parse('MAIN.TEST_ROW');
    }
}

The TPL:

XML/XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<!-- BEGIN: MAIN -->
 
<div id="test_div">
<!-- BEGIN: TEST_ROW -->
    {TEST_USERNAME}
    {TEST_MESSAGE}
    {TEST_DATE}
    {TEST_DATETIME}
<!-- END: TEST_ROW -->
</div>
 
<!-- END: MAIN -->

For some reason it just doesn't display what I assign to MAIN.TEST_ROW.
What am I missing here?

When I do a simple echo for testing like shown below it does output the data in the TPL with AJAX.

PHP
1
2
3
4
5
if ($a == 'update') {
    foreach ($res->fetchAll() as $row) {
        echo $row['test_message'];
    }
}

Thanks.

Added 1 hours later:

Hmm, it sort of works when I add to the foreach loop both parse and out:

PHP
1
2
$t->parse('MAIN.TEST_ROW');
$t->out('MAIN.TEST_ROW');

But I'm wondering if this is the correct way?

Added 2 days later:

Bump.

Отредактировано: Twiebie (03.10.2012 17:46, 12 лет назад)
GHengeveld
#2 04.10.2012 06:52

Within the foreach, you need to do a parse('MAIN.TEST_ROW'), not an out().

At the end of your file, you need to do this:

PHP
1
2
$t->parse('MAIN');
$t->out('MAIN');

Usually you use out() only once: for the MAIN block at the end of your php code.

You can omit the 'MAIN' in out(), because it's the default value. To seperate your html code for a=update you may want to add the UPDATE block. Also, you'll often want to be able to show a message if there aren't any results:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if ($a == 'update') {
    $rows = $res->fetchAll();
    if ($rows) {
        foreach ($rows as $row) {
            $t->assign(array(
                'TEST_USERNAME' => cot_build_user($row['test_userid'], htmlspecialchars($row['test_username'])),
                'TEST_MESSAGE' => cot_parse($row['test_message'], $cfg['plugin']['comments']['markup']),
                'TEST_DATE' => cot_date('M jS', $row['test_date']),
                'TEST_DATETIME' => cot_date('G:i', $row['test_date'])
            ));
            $t->parse('MAIN.UPDATE.ROWS.ROW');
        }
        $t->parse('MAIN.UPDATE.ROWS');
    } else {
        $t->parse('MAIN.UPDATE.NO_ROWS');
    }
    $t->parse('MAIN.UPDATE');
}
$t->parse('MAIN');
$t->out();

A complete tpl could look like this:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- BEGIN: MAIN -->
<!-- BEGIN: UPDATE -->
<div id="update">
  <!-- BEGIN: ROWS -->
  <table>
    <thead>
      <tr>
        <th>Username</th>
        <th>Message</th>
        <th>Date</th>
        <th>Datetime</th>
      </tr>
    </thead>
    <tbody>
      <!-- BEGIN: ROW -->
      <tr>
        <td>{TEST_USERNAME}</td>
        <td>{TEST_MESSAGE}</td>
        <td>{TEST_DATE}</td>
        <td>{TEST_DATETIME}</td>
      </tr>
      <!-- END: ROW -->
    </tbody>
  </table>
  <!-- END: ROWS -->
  <!-- BEGIN: NO_ROWS -->
  None
  <!-- END: NO_ROWS -->
</div>
<!-- END: UPDATE -->
<!-- END: MAIN -->
Отредактировано: GHengeveld (04.10.2012 07:37, 12 лет назад)