Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Renze on
i have 2 selectbox's in the first selectbox there is a list of country's from db.
When i select a country i want the second selectbox to fill with city's.
So i want a onchange action on the first selectbox. When a selection is made, i want function city() to be called to fill en visualize the second selectbox.
$form['drop'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#options' => $countryArray,
'#attributes' => array('onChange' => 'dropdown_onclick_function()),
);
What do have i put in the attributes onchange, and how do i get the value from the first selectbox to return?
Comments
re: try this
Try the solution here with an example here
That would work if i was
That would work if i was running drupal 5.x, But i am currently running drupal 4.7 and than the Jquery code gives errors.
There must be a easy way to this.
something like this:
My question:
- How do i Call a function with onchange?
- How do i give the value of my drop box with it?
jQuery Module
There is a module for Drupal 4.7.x which is basically a copy of jQuery modified so that it doesn't produce any errors with Drupal's own JS. I think it's called the jQuery module.
Dave
Dynamic Select box in Drupal 5
Hi
I have one working solution for this problem with JavaScript
Here is it
In my custom module , i have to load the Caste of corresponding Religion
my select box definition is
$form['_religion']=array(
'#type' => 'select', // religion select box
'#title' => t('Religion'),
'#default_value' => '',
'#attributes' => array('onclick'=>"htmlData('caste.php', 'ch='+_religion[selectedIndex].text)"),
'#options' => $religion,
);
$caste= array("Any" =>"Any");
$form['_caste']=array(
'#type' => 'select',
'#title' => t('Caste'),
#'#default_value' =>array("Any"),
'#options' =>$caste ,
'#prefix' => '
'#suffix' => '
'
And i have included the Java Script file for the function htmlData() directly into my page.tpl.php
like
here is the Java script
########################SUJITH S #############################
function GetXmlHttpObject(handler)
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("caste_choice").innerHTML= xmlHttp.responseText;
}
else {
//alert(xmlHttp.status);
}
}
// Will populate data based on input
function htmlData(url, qStr)
{
if (url.length==0)
{
document.getElementById("caste_choice").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
url=url+"?"+qStr;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true) ;
xmlHttp.send(null);
}
#######################KERALA#########################
the URL call the caste.php file in Drupal directory
HERE IS the caste.php
################SUJITH#################################
THIS worked for me , I think this will help you solve same or related problem (replacing Religion and caste with , Country and city ) for those who spending a lot of time to solve
Please Let me know if any body knows other good solution than this
Free software Free community
this story is created by Sujith S (Little Penguin)
SUJITH
I have a same problem in Drupal 6. I need to pass the data from text field and validate through ajax as you did. Do u have it for Drupal 6?
Suresh.E
Am a Acquia Certified Developer--Life Knocks us down. we have to decide whether or not to get up--
example gone
That solution and example are gone. Anyone have something similar?
You should use jquery.
You should use jquery. Something like that:
$(document).ready(function {
$('.select').change(function{
});
});