Importing data into combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mainul
    New Member
    • Sep 2006
    • 51

    Importing data into combo box

    Hi,
    i have a mysql database 'country_list' which consist of two entity- one is country_code another is country_name.no w i need to import all the country_code into a combo box and when a user will select a country code the respective country name will be displayed into a text box. how can i do that?

    waiting for ur reply.

    thanks
    Mainul
  • ctsasikumar
    New Member
    • Nov 2006
    • 17

    #2
    Hai Using ajax concept

    [PHP]
    file name:country_co de.php
    <?
    $connect=mysql_ connect("localh ost","root","") ;
    $select_db=mysq l_select_db("co untry",$connect );
    ?>
    <SCRIPT LANGUAGE="JavaS cript">
    <!--
    var req
    function gettest(key)
    {
    //alert(key);
    req = GetXmlHttpObjec t();
    req.open('POST' ,'getdatbase.ph p', false);
    req.setRequestH eader('Content-type', 'application/x-www-form-urlencoded;char set=UTF-8;');
    //req.onreadystat echange=stateCh anged
    req.send('ids=' +key);
    if(req.status == 200)
    document.getEle mentById("rr"). value=req.respo nseText;
    // document.getEle mentById("r").i nnerHTML=req.re sponseText;
    }
    function stateChanged()
    {
    /*if (req.readyState ==0)
    {
    //alert("loding") ;
    document.getEle mentById('conte nt').innerHTML= ""
    }
    else if (req.readyState ==1)
    {
    //alert("loding") ;
    document.getEle mentById('conte nt').innerHTML= "LOADING"
    }
    else if (req.readyState ==4)
    {
    //alert("loding") ;
    document.getEle mentById('conte nt').innerHTML= "completed"
    }*/
    }
    function GetXmlHttpObjec t()
    {
    var objXMLHttp=null
    if (window.XMLHttp Request)
    {
    objXMLHttp=new XMLHttpRequest( )
    }
    else if (window.ActiveX Object)
    {
    objXMLHttp=new ActiveXObject(" Microsoft.XMLHT TP")
    }
    return objXMLHttp
    }
    //-->
    </SCRIPT>

    <table align=center width=80% border=1>
    <form name=test method=post action="">
    <tr><td>Selec t Country name</td><td><select name="country" onChange="gette st(this.value)" >
    <?
    $select_value=" Select * from coun";
    $exexute_quey=m ysql_query($sel ect_value);
    $num_rows=mysql _num_rows($exex ute_quey);
    for($i=0;$i<$nu m_rows;$i++)
    {
    $getdata=mysql_ fetch_array($ex exute_quey);
    $country_code=$ getdata['cid'];
    $country_name=$ getdata['cname'];
    ?>
    <option value=<?=$count ry_code?>><?=$c ountry_name?></option>
    <?
    }
    ?>
    </select>
    </td></tr>
    <tr><td>Count ry name</td><td><input type=text id="rr" ></tr>
    <tr><td><span id="r"></span></td></tr>
    <tr><td colspan=1><inpu t type=submit name=sub value=submit></td>
    </form>
    </table>


    filename:getdat base.php
    <?
    $connect=mysql_ connect("localh ost","root","") ;
    $select_db=mysq l_select_db("co untry",$connect );
    $couid=$_REQUES T['ids'];
    $select_value=" Select * from coun where cid=$couid";
    $exexute_quey=m ysql_query($sel ect_value);
    $getdata=mysql_ fetch_array($ex exute_quey);
    echo $countryname=$g etdata['cname'];
    ?>
    [/PHP]

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      This looks like a simple problem with a simple solution. I believe Ajax is useful, but not a cure for all ailments and certainly not in this case.

      There is (a) a 1-1 relation between the country code and the country name and (b) you display all the country codes already in the box, so there is no need for any fancy and heavy solution. You can use a simple dropdown box that holds all data.

      Make one drop down select box with the country name as the value and the country code as the text part of the option. As follows:
      Code:
      <option value='country_name'>country_code</option>
      The user selects the country code presented to him/her and the value of the selected box (i.e. country_name) is passed to you and you can display it onto the screen.

      Ronald :cool:

      Comment

      • mainul
        New Member
        • Sep 2006
        • 51

        #4
        hello guys thank you for your solutions

        Comment

        Working...