PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 22, 2014 5:02 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 
Offline
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-container').empty().addClass('ajax-loading');
     
    //Ajax Request start here
    var myElement = document.getElementById('ajax-container');
    var cid = document.getElementById('jform_country').value;
    //alert(cid);
    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...');
        myElement.addClass('loading');
    },
    onSuccess: function(responseText){
        myElement.set('html', responseText);
        myElement.addClass('success');
    },
    onFailure: function(){
        myElement.set('text', 'Sorry, your request failed <img src="./images/smilies/icon_sad.gif" alt=":(" title="Sad" />');
        myElement.addClass('error');
    }
}).send();
 } );
});


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);
        $query->select('title')
            ->from('#__iproperty_states')
            ->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.

Thanks.


Top
 Profile  
 
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: No registered users and 7 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