PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Tue Apr 24, 2018 11:48 pm

All times are UTC - 5 hours

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Ajax and php in Joomla
PostPosted: Tue Jun 26, 2012 5:23 pm 
Forum Newbie

Joined: Tue Jun 26, 2012 5:03 pm
Posts: 1
Hi everyone! My first post :D

I was hoping you knowledgeable folks would be able to help me. Im not a developer by any means, ive just recently started dabbling with php.. Im using joomla 2.5 and am using a component which has a country and state field.. it is a basic property listing website and the database structure has a separate countries table but no separate states table.. so i duplicated the countries table and renamed relevant data to states and changes the values... Fine. Now the property entry form has a drop down country select but a textbox for the states. Im trying to populate the states in a dropdown based on the country selected using ajax. I have additionally created extra tables in the countries and states tables in the db, called cat_id.

Now, in the component/views i found this
Syntax: [ Download ] [ Hide ]
<div class="formelm"><?php echo $this->form->getLabel('locstate'); ?>
<?php echo $this->form->getInput('locstate'); ?></div>
<div class="formelm"><?php echo $this->form->getLabel('country'); ?>
<?php echo $this->form->getInput('country'); ?></div>

so after reading up a bit on ajax etc, i modified it to include this :
Syntax: [ Download ] [ Hide ]
<span id="ajax-container">form->getInput('states'); ?></span></li>

I then thought i needed to include the onchange event.. so this then came up..

Syntax: [ Download ] [ Hide ]
document.addEvent( 'domready' ,  function() {
$('jform_country').addEvent( 'change' ,  function() {
    //Ajax Request start here
    var myElement = document.getElementById('ajax-container');
    var cid = document.getElementById('jform_country').value;
    var myRequest = new Request({
    url: 'index.php?option=com_country&view=items&task=getstates&format=raw',
    method: 'get',
    evalResponse: 'true',
    data: {
    'id' : cid,    
    onRequest: function(){
        myElement.set('text', 'Loading. Please wait...');
    onSuccess: function(responseText){
        myElement.set('html', responseText);
    onFailure: function(){
        myElement.set('text', 'Sorry, your request failed <img src="./images/smilies/icon_sad.gif" alt=":(" title="Sad" />');
 } );

I then found this in a html.helper file located in the component/helpers/ folder which had this code :

Syntax: [ Download ] [ Hide ]
    function getstateName($state)
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            ->where('id = '.(int)$city);

        $db->setQuery($query, 0, 1);
        $result = $db->loadResult();

        return $result;

now thats as far as i got.. i dont know how to proceed to select the states based on the country selected and load them on the dropdown... im sorry if its all confusing.. but i could really use a little guidance and help.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: Majestic-12 [Bot] and 22 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group