| Twiebie |
|
|---|---|
|
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:
$(document).ready(function() {
function updateTesting(){
ajaxSend({
method: "GET",
url: 'index.php?r=test&a=update',
data: "",
divId: 'test_div'
});
}
updateTesting();
And test.ajax.php:
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:
<!-- 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. When I do a simple echo for testing like shown below it does output the data in the TPL with AJAX.
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:
$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, 13 лет назад)
|
| GHengeveld |
|
|---|---|
|
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:
$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:
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:
<!-- 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, 13 лет назад)
|