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) »
  • Add Ajax Event on postal code field
Pages: [1]

Author Topic: Add Ajax Event on postal code field  (Read 4276 times)

matast

  • Guest
Add Ajax Event on postal code field
September 23, 2009, 08:26:50 am
Hi !

I have a little problem in the "add contact form". I'd like to catch the onkeyup event on the postal code field to fill in the city field using Ajax. I saw in sites/all/modules/civicrm/CRM/Contact/Form/Address.php that I can add an attribute on the postal code field, but do u know how I can use a civicrm ajax framework to fill in my target field ? I imagine I have to use JQuery...

Thanks a lot for your help !

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Add Ajax Event on postal code field
September 23, 2009, 08:43:22 am

yes, you will have to use jQuery

You will also need another table that gives you a zipcode -> city name value. check the sql/zipcodes.mysql file in the distribution

while u r at this, you might also want to populate the state and the country for that address :) Please publish your code when done

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

matast

  • Guest
Re: Add Ajax Event on postal code field
September 23, 2009, 01:52:02 pm
Thanks,

Ok for jQuery, I usually use ExtJS but I will learn jQuery, that's not a real problem. But I don't know how to use it in CiviCrm. I just know that I can add a key/value pair in the $attributes like 'onkeyup' => 'javascript:myfunction();' but where do I create my customize function ? Do I have to load some jQuery components ? In this case where ? Is there a civicrm method to use ?

Thanks a lot.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Add Ajax Event on postal code field
September 23, 2009, 02:45:10 pm

1. check: http://jqueryhelp.com/viewtopic.php?p=832&sid=7d0931ab91b60e91b40e34b1300dd7f2

2. customize the templates and add your custom javascript there or in another include file

3. we dont have a zipcode -> city/state/country map, so you'll need to write custom php to handle this and expose it via a url (so u can call it from the JS)\

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

matast

  • Guest
Re: Add Ajax Event on postal code field
September 25, 2009, 09:42:18 am
It ok for jquery (thanks for the link !),
ok for the custom javascript function in my template,
ok for calling a php script that gets informations in database.

But now I've another probleme with this php script called by jquery. It's out of any context and when I want to use some civiCrm functions like CRM_Core_DAO to access database, I get fatal errors.

Do I have to include each class I need or is there a way to include a group of classes or something like that ?

Thanks a lot.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Add Ajax Event on postal code field
September 25, 2009, 10:36:23 am

you will have to initialize CiviCRM and include the specific files you are calling. Check some of the scripts in the bin/ directory to see easy ways of initializing civicrm by an independent url

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

LakeGirlMT

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.7
  • CMS version: 4.4.2 wordpress
  • MySQL version: MariaDB equivalent to MySQL 5.7
  • PHP version: not sure
Re: Add Ajax Event on postal code field
March 14, 2016, 06:47:14 am
I apologize for commenting in this old thread, but I was just wondering if this was ever implemented successfully and if the code was posted somewhere. I need to replace a very antiquated CRM and am thinking of using CiviCRM but one of the requested features is to auto-populate the city and state when the zip code is entered.

Thanks for any help in advance.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Add Ajax Event on postal code field

This forum was archived on 2017-11-26.