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 » symfony 1.3 and 1.4 » Correct way to implement JQuery ajax?
Correct way to implement JQuery ajax? [message #102418] Tue, 13 July 2010 13:17 Go to next message
rob.davidson84  is currently offline rob.davidson84
Messages: 15
Registered: June 2010
Junior Member
Hi,

Im no sure if the way im going about it is the right way.

Im planning on modifiying existing actions that already work in php to include ajax rather than writing a new action solely for ajax? Is this bad pratice?

I am using symfony 1.4 with doctrine.


javascript file

strData = $('#customerUpdateBarcode').serialize()+'&ajax=true';  //parses the data fields

strURL = $('#customerUpdateBarcode').attr('action'); //get the url to post data to

$.ajax({
type: "POST",
url: strURL,
async: false,
data: strData,
success: function(msg) 
{
	// output the array of saved barcodes
	alert(msg);
}
});



And in the action function it will look for $_REQUEST['ajax'] and carry out a modified output (namely an array of modified barcodes and/or some with errors for user feedback)



Re: Correct way to implement JQuery ajax? [message #102459 is a reply to message #102418 ] Wed, 14 July 2010 11:22 Go to previous messageGo to next message
stormsson  is currently offline stormsson
Messages: 211
Registered: April 2009
Faithful Member
to achieve this result i suggest you to use jqueryreloaded plugin, wich is absolutely simple and quick to put up, and its helepr method jq_link_to_remote ( or something else if you do not need a link, it is almost identical to the old javascriptHelper )

after that, in the action you could simply check if you are reaching the action by ajax, with the method

$request->isXmlHttpRequest()
Re: Correct way to implement JQuery ajax? [message #102462 is a reply to message #102418 ] Wed, 14 July 2010 12:29 Go to previous messageGo to next message
rob.davidson84  is currently offline rob.davidson84
Messages: 15
Registered: June 2010
Junior Member
Thankyou for your reply.
I have chopped everything away for the example code below. Is this how it would be implemented?


Action function()

function executeSave()
{
  if($request->isXmlHttpRequest())
  {
    // is this right??
    echo json_encode(some data);
    exit()
  }
  else
  {
  //Do the normal action function
  }
}



Template file

  //not sure if i have right idea here
  echo jq_link_to_remote(image_tag('icons/save.png'), array('url'=> '@save')),
 	                                   'dataType' => 'json',
 	                                   'success'  => 'save(request)')) 


Javascript File
$('#customerUpdateBarcode').bind('submit', function(event){	
     
     arrData = ???  How do I get the json encoded data so I can apply some javascript magic to it?? 
		

     //is this still needed to stop default action??		 
     event.preventDefault();		 
});

Re: Correct way to implement JQuery ajax? [message #102466 is a reply to message #102462 ] Wed, 14 July 2010 14:25 Go to previous messageGo to next message
xplo  is currently offline xplo
Messages: 428
Registered: September 2008
Faithful Member
helper for javascript ajax are kind of droped in the core since sf 1.2.
Using directly jQuery is totally fine and readable. I really dont see the need to use/learn a plugin outputing javascript code with php code.

Just use isXmlHttpRequest method instead of your ajax variable added and you re fine.
Re: Correct way to implement JQuery ajax? [message #102470 is a reply to message #102418 ] Wed, 14 July 2010 14:43 Go to previous messageGo to next message
stormsson  is currently offline stormsson
Messages: 211
Registered: April 2009
Faithful Member
there is no "need", i simply find it quicker.

My 2c.



the workflow could be something similar:

action

Action function()

function executeSave()
{
  if($request->isXmlHttpRequest())
  {
    $this->setTemplate('ajaxSave'); // create a template with the output you need if it is complex, the template will be returned by the ajax call

$this->setLayout(false); //usually you'll need to remove the layout too
  }
  else
  {
  //Do the normal action function
  }
}


template where the ajax call is done:
<?php echo jq_link_to_remote('My link', array('script'=>1, 'update'=>'div_id_to_update','type'=>'POST', 'url'=> url_for('@myroute?id='.$box->getId()))) ?>





btw this is not the bible, it's simply the way i find quicker.

In fact xplo is right telling that the javascript helper has been removed from the core, in my case i took again that helper and put in my project dir, since i PERSONALLY find it quicker than re-create the helpers by hand, or skipping them..

I don't know / understand why it is so hated.
Re: Correct way to implement JQuery ajax? [message #102472 is a reply to message #102418 ] Wed, 14 July 2010 14:53 Go to previous message
rob.davidson84  is currently offline rob.davidson84
Messages: 15
Registered: June 2010
Junior Member
Thanks your your replies guys. Some good pointers

I think the helper would come in handy depending on how much it will reduce the amount of code you ultimately use.

Keeping the javascript inside the javascript file seems like the most sensible way forward. As it separates the php and JavaScript logic.





Previous Topic:session_start() failed to initialize storage module
Next Topic:symfony filters
Goto Forum:
  

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