Home | Docs  
 
 
   
  Categories



Home
  -  Core methodology -
AJAX

AJAX


AJAX

SP-Cart 2.0 allows to process AJAX requests (AR) that make it possible to retrieve necessary data from a server and, if necessary, display it without a page reload.
 
 
 
 
 
 
 
   

General

To work with AJAX requests (AR) the program uses its own extension of the standard AJAX functionality of the jQuery library - /js/ajax.js. The class has three main methods:

  • ajaxRequest – used to send AR to a server through a URL.
  • ajaxSubmit - used to send AR to a server through a form.
  • ajaxResponse – used to handle the server response and display the result.

Data handling is carried out in JSON format.

When processing AR on the server initialization of the AR handling subsystem is performed by the fn_init_ajax(/core/fn.init.php) function:

  • an object of AJAX class(/core/class.ajax.php) is created; it performs necessary transformations before and after AR and also generates JSON array for further transfer to ajaxResponse.
  • the constant AJAX_REQUEST is set to true for use in PHP code

Using the constant AJAX_REQUEST in a PHP file you can specify the code that is required to be executed only on AR.

Example.

1.
2.
3.
 if (defined('AJAX_REQUEST')) {
         fn_set_notification('E', fn_get_lang_var('warning'), $msg, true, 'insecure_password');
 }

 

 

When execution of the AJAX request script is completed or when the termination function exit() is called explicitly, the destructor of AJAX class (/core/class.ajax.php) is executed. The destructor passes the information that should be displayed in the document to the array with data in JSON format and then passes the array to the JS function ajaxResponse(/js/ajax.js).

 

AR through microformat

So that a link at the click or a form when sending initiates an AR, you can use format cm-ajax.
Example 1. AR at the click of a link (GET method).

 <a class="underlined-bold cm-ajax"
href="index.php?subcats=Y&dispatch=products.search&sort_by=product&sort_order=desc"
coords="my_div" rel="nofollow">Product</a>
 

 

 

 

The parameter coords contains a list of ID HTML elements (separated by comma, if there are several ones), the content of which will update this request.

Example 2. AR for sending a form (POST method).

 <form class="cm-ajax" action="index.php" method="post" name="product_form_817">
 <input type="hidden" name="result_ids" value="my_id" />
 ...
 <input id="button_cart_817" type="submit" name="dispatch[checkout.add..817]" value="Add to cart" />
 ...
 </form>

 

 

 

Hidden input result_ids contains a list of ID HTML elements (separated by comma, if there are several ones), the content of which will update this request.

cm-ajax microformat processing takes place in the file /js/core.js. Before sending a request to the server, the parameter is_ajax = true; is added to the data. This parameter indicates that the request is to be processed as an AR.

 

AR through ajaxRequest

If according to an event, before sending an AR it is necessary to execute some extra JS code, then the AR can be processed by the function ajaxRequest.

Example.

<input id="enable_block_1" type="checkbox" name="enable_block_1" value="Y" 
onclick="jQuery.ajaxRequest('{$index_script}?dispatch=block_manager.enable_disable&amp;
block_id=1&amp;enable=' + (this.checked ? this.value : 'N'), {literal}{method: 'POST', cache: false}
{/literal});"
/>

 

 

Direct use of the ajaxSubmit method is also possible, but is not applied in practice.

 

Additional

There is the ability to define pre-call and callback functions for forms (only for ajax forms). To do this it is necessary to declare a function, which name is defined according to the following rules:

  1. Pre-call: 'fn_form_pre_form_name'. The function should return true/false. If false is returned – the form will not be submitted.
  2. Call-back: 'fn_form_post_form_name'. The object response.data is passed to the function as a parameter.

Function Pre-call is called by event onSumbit, but before actual sending of data. Function Callback will be called after a response from AR is received.

     
              << Read Previous                                                                                                        Read Next>>
     
Copyright © 2009 SP-CART.COM. All Rights Reserved.