Ajax and php in Joomla

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Ajax and php in Joomla

Postby R-Dawkins » Tue Jun 26, 2012 5:23 pm

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.
R-Dawkins
Forum Newbie
 
Posts: 1
Joined: Tue Jun 26, 2012 5:03 pm

Return to PHP - Code

Who is online

Users browsing this forum: Google [Bot] and 14 guests