Here is a very simple and useful code.
(original code: http://cakebaker.42dh.com/2006/06/29/how-to-update-
multiple-divs-with-ajax/ )

1. sparks_controller.php

<?php
class SparksController extends AppController {
  var $name = 'Sparks';
  var $layout  = 'spark';
  var $helpers = array('Html', 'Javascript', 'Ajax');

function index()
{
}

function update()
{
        sleep(1);
        $this->layout = 'ajax';
}

}
?>

2. index.thtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; lang="ko" xml:lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
</head>
<body>
<?php echo $ajax->link('Link', '/sparks/update', array('update' =>
array('first', 'second'))); ?>
<div id="first">first div</div>
<div id="second">second div</div>
</body>
</html>

3. update.thtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; lang="ko" xml:lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title> Search Results</title>
</head>
<body>
<?php echo $ajax->div('first'); ?>
first div updated
<?php echo $ajax->divEnd('first'); ?>
<?php echo $ajax->div('second'); ?>
second div updated
<?php echo $ajax->divEnd('second'); ?>
</body>
</html>

4. sparks.thtml (layout)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title>Ajax Test :: <?php echo $title_for_layout?></title>
<?php echo $html->charsetTag('UTF-8')?>
<?php echo $javascript->link('prototype')?>
<?php echo $javascript->link('scriptaculous')?>
</head>
<body>
<div id="container">
    <center><h2>Ajax Test</h2></center>
    <div id="content"><?php echo $content_for_layout?></div>
</div>
</body>
</html>

The preceding code runs great when English strings such as "first div"
"first div updated" are used. I mean when 'Link' is clicked, "first
div" is succefully updated  to "first div updated", and "second div"
to "second div updated" simultaneously.
But the ouput gives weird characters when non-English characters are
used in update.thtml. For example, if "first div updated" is replaced
with some Korean words(UTF-8 Korean or EUC-KR Korean) in update.thtml,
the ouput gives weird characters. I didn't try, but other languages
such as Japanese, Chinese, and some European languages would suffer
from the same problem. Although I found some related threads here, but
they did not help me greatly.

The next link I came across seems to give some clues to the preceding
problem, but currently I cannot figure out how to modify ajax.php and
my preceding codes. Could anyone please help me?

https://trac.cakephp.org/ticket/1244

"If you use ajax update for multiple divs, you have to use ajax->div
and ajax->endDiv functions. These are end up in a response containing
a script. the contents of these divs are rawurlencoded at the server
side, and unescaped at the clientside. This mechanism works great
without utf8 chars, but produce some weird character with utf8 char.

The problem can be fixed if I use the script found at
http://www.webtoolkit.info/javascript/url-encode-decode/ of course the
ajax.php needs to be modified."


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to