CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Speeding up ajax country/state call
Pages: [1]

Author Topic: Speeding up ajax country/state call  (Read 823 times)

mhenning

  • Guest
Speeding up ajax country/state call
March 23, 2010, 08:36:41 pm
I was wondering if anyone knew of a way to speed up the population of the state dropdown box on the create contact->address section when you select a new country.

When a new country is selected it makes an ajax call to this url: /civicrm/ajax/jqState?_id=address_1_country_id&_name=address[1][country_id]&_value=1228.  It returns an array used to populate the state dropdown box.

The problem is that it takes around 2 seconds on our drupal site to do this.  I believe this is because the ajax call must first load all the drupal and civicrm framework (which slows it down). 

The client is complaining and I was wondering if anyone had run into this and had any suggestions on how to improve the speed. 

Thanks,
Matt


mhenning

  • Guest
Re: Speeding up ajax country/state call
March 24, 2010, 08:26:57 pm
Ok, so I created a solution and I will share it here in case someone else might like it.

First I added a line to the .htaccess file:
RewriteRule ^civicrm/ajax/jqState?(.*)$ sites/all/modules/custom_module/ajax_jqState.php?$1 [L,QSA]

Then I created the sites/all/modules/custom_module/ajax_jqState.php file with this code:
if ( ! isset( $_GET['_value'] ) || empty( $_GET['_value']) || !is_numeric( $_GET['_value'] ) ) {
   exit();
}

chdir('../../../../');
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

$result = db_query('SELECT name, id FROM {civicrm_state_province} WHERE country_id= %d', $_GET['_value']);

$elements = array( array( 'name'  => '- select a state-', 'value' => '' ) );
while ($row = db_fetch_object($result)) {
   $elements[] = array( 'name'  => $row->name, 'value' => $row->id );
}

echo json_encode( $elements );
exit();

Works much quicker now.


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Speeding up ajax country/state call

This forum was archived on 2017-11-26.