Ajax and php in Joomla
Posted: Tue Jun 26, 2012 5:23 pm
Hi everyone! My first post
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
so after reading up a bit on ajax etc, i modified it to include this :
I then thought i needed to include the onchange event.. so this then came up..
I then found this in a html.helper file located in the component/helpers/ folder which had this code :
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.
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
Code: Select all
<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>Code: Select all
<span id="ajax-container">form->getInput('states'); ?></span></li>Code: Select all
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 :(');
myElement.addClass('error');
}
}).send();
} );
});Code: Select all
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;
}Thanks.