This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » JSON - multiple ajax calls - revisited
JSON - multiple ajax calls - revisited [message #8109] Thu, 29 June 2006 04:00 Go to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
with JSON, what if i want to populate multiple divs with other php pages rather than just a string? as in the normal link_to_remote..

any ideas?

thanks!
Re: JSON - multiple ajax calls - revisited [message #8118 is a reply to message #8109 ] Thu, 29 June 2006 13:18 Go to previous messageGo to next message
francois  is currently offline francois
Messages: 1636
Registered: October 2005
Faithful Member
What is a "php page"? You mean a partial, for instance?
Re: JSON - multiple ajax calls - revisited [message #8131 is a reply to message #8118 ] Thu, 29 June 2006 15:40 Go to previous messageGo to next message
vonLeeb  is currently offline vonLeeb
Messages: 74
Registered: March 2006
Member
Yeap, ditto.

JSON works ok when I want to change a string that is displayed in particular div. It doesn't work when I need to update for example graphics or a partial. In that case I got to the idea to join JSON header with classic return sfView::SUCCESS, to replace a partial and graphics by partial and replace some strings (like counters for exmaple) by JSON. It works however still doesn't solve a problem when more than one partial or graphics need an update.

BTW. I followed wiki example for JSON. Maybe with some additional java development it could be possible to update more than just a string however I'm not java guru to do that.

vl
Re: JSON - multiple ajax calls - revisited [message #8155 is a reply to message #8109 ] Fri, 30 June 2006 01:30 Go to previous messageGo to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
see i need to update two <div>s both with partials..

can anyone see a work around?
Re: JSON - multiple ajax calls - revisited [message #8178 is a reply to message #8155 ] Fri, 30 June 2006 11:59 Go to previous messageGo to next message
noel  is currently offline noel
Messages: 62
Registered: January 2006
Location: france, 94
Member
Hi,

Try that:
public function executeTest(){
$firstDiv = $this->getPresentationFor('module', 'action');
$secondDiv = $this->getPresentationFor('module', 'other_action');
$output = sprintf('[["firstDiv","%s"],["secondDiv","%s"]', $firstDiv, $secondDiv);

$this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
return sfView::HEADER_ONLY;
}


hope this helps

noel
Re: JSON - multiple ajax calls - revisited [message #8179 is a reply to message #8178 ] Fri, 30 June 2006 12:12 Go to previous messageGo to next message
noel  is currently offline noel
Messages: 62
Registered: January 2006
Location: france, 94
Member
if you want the result of a partial instead of the result of an action (like in my previous post), you can do that:

require_once('symfony/helper/PartialHelper.php');
$this->firstDiv = get_partial('beta/test', $vars);
Re: JSON - multiple ajax calls - revisited [message #8275 is a reply to message #8179 ] Tue, 04 July 2006 04:36 Go to previous messageGo to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
thanks for your response noel!

the getPresentationFor will do the trick..

however, i still can't get it going..

this is what i have currently:

public function executeRefresh()
  {
      $firstDiv = $this->getPresentationFor('events', 'racinghome');
      $secondDiv = $this->getPresentationFor('leftnav', 'racingcompetition');
      $output = sprintf('[["content1","%s"],["content2","%s"]]', $firstDiv, $secondDiv);
    $this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
    return sfView::HEADER_ONLY;
  }



and in the view:
<?php echo use_helper('Javascript'); ?>

<div id="content1">
content 1
</div>
<div id="content2">
content 2
</div>
<?php echo link_to_remote("refresh", array("url" => "/leftnav/refresh", 'complete'=>'updateJSON(request,json)') ); ?>


can you see any real flaws here??


and one more thing Smile

even the most basic example on http://www.symfony-project.com/trac/wiki/AjaxAndJSON doesn't work in IE or Safari... what the??

thanks for any help!!!!!

[Updated on: Tue, 04 July 2006 04:37]

Re: JSON - multiple ajax calls - revisited [message #8287 is a reply to message #8275 ] Tue, 04 July 2006 10:00 Go to previous messageGo to next message
noel  is currently offline noel
Messages: 62
Registered: January 2006
Location: france, 94
Member
Hi,

Have you js errors?

I suggest you to use the class Service_JSON (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) to encode X-JSON headers.It allows you to escape some specials characters like \r, \n & co, which cause some problems sometimes.

noel
Re: JSON - multiple ajax calls - revisited [message #8690 is a reply to message #8109 ] Wed, 12 July 2006 01:21 Go to previous messageGo to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
thanks for all your help so far noel, however i am still unable to get this working properly..

is there any other method to calling multiple partials into divs?

or if you think that this is the only real method, is it possible to get a code example? apologies for being so demanding, however i've tried my best to get this running with no luck.,.

thanks@
Re: JSON - multiple ajax calls - revisited [message #8725 is a reply to message #8690 ] Wed, 12 July 2006 15:41 Go to previous messageGo to next message
noel  is currently offline noel
Messages: 62
Registered: January 2006
Location: france, 94
Member
Hi,

Try that:

action.class.php
public function executeUpdateDivs(){
  	$this->firstDiv = $this->getPresentationFor('beta', 'firstDiv');
  	$this->secondDiv = $this->getPresentationFor('beta', 'secondDiv');
  	$json = new Services_JSON();
        $output = $json->encode($this->getVarHolder()->getAll());
  	$this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
    return sfView::HEADER_ONLY;
  }
  
  public function executeIndex(){}  

  public function executeFirstDiv(){}
  
  public function executeSecondDiv(){}


indexSuccess.php
<?php use_helper('Javascript'); ?>
<script language="JavaScript" type="text/javascript">
  function updateJson(request, json){
 		for (var prop in json){
 		Element.update(prop, json[prop]);
 		}
  }
</script>
  
<?php echo link_to_remote('update', array('url' => 'beta/updateDivs', 'complete' => 'updateJson(request, json)')); ?>
<div id="firstDiv">not updated</div>
<div id="secondDiv">not updated</div>


Next, put some text in firstDivSuccess.php and secondDivSuccess.php, don't forget to create updateDivsHeaders.php and it works.

noel
Re: JSON - multiple ajax calls - revisited [message #8753 is a reply to message #8725 ] Wed, 12 July 2006 20:43 Go to previous messageGo to next message
vonLeeb  is currently offline vonLeeb
Messages: 74
Registered: March 2006
Member
May I ask why we need updateDivsHeaders.php and what is the role of that file and what should be in?
It's the first time I hear of that. Did I miss some docs about it?

Keep education effort noel, it's really nice to hear from you. Thanks.

vl
Re: JSON - multiple ajax calls - revisited [message #8758 is a reply to message #8725 ] Thu, 13 July 2006 00:55 Go to previous messageGo to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
Noel,

Thankyou very much for the detailed response!

I too am not sure what you mean with the updateDivsHeaders.php

Are you refering to the JSON class?

Thanks
Re: JSON - multiple ajax calls - revisited [message #8759 is a reply to message #8109 ] Thu, 13 July 2006 02:05 Go to previous messageGo to next message
replicah  is currently offline replicah
Messages: 43
Registered: June 2006
Member
ok, i've got it working now in FireFox..

But still no luck in IE and Safari

I think it is related to this js:

function updateJson(request, json){
 	for (var prop in json){
 	Element.update(prop, json[prop]);
 }
  }


Any thoughts?
Re: JSON - multiple ajax calls - revisited [message #8764 is a reply to message #8759 ] Thu, 13 July 2006 10:20 Go to previous message
noel  is currently offline noel
Messages: 62
Registered: January 2006
Location: france, 94
Member
I've try it with IE 5.5+, and it works fine. I can't try with safari now.

And about updateDivsHeaders.php, it's a mistake: I'm using a custom executionFilter which has broken the correct execution when I use the render sfView::HEADER_ONLY. Sad

noel
Previous Topic:[resolved] filter on foreign key
Next Topic:Symfony Security issue - XSS attack on form helpers
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software