Welcome Guest, Not a member yet? Register   Sign In
Populate query result to drop down list with Ajax
#1

[eluser]kuanfai[/eluser]
Hi, I think this topic has been discussed before however I can't find the solution for my problem. There are two drop down list in my html form, the contents of "contest" drop down depends on the selected option from "store" drop down box.

Code:
<?php
    $default = 'Please Select';
    $options = array($default=>$default, 'value1'=>'value1',);
    $choosestore = 'onChange="doWork(this.value)"';
?>
<table align="center" class="table_noborder">
    <tr>
        <td colspan="3" align='left'><h3>Create Configuration</h3></td>
    </tr>
    <tr>
        <td align='left' class='td1'>Store</td>
        <td colspan='2' align='left'>&lt;?php echo form_dropdown('store', $options, $default, $choosestore);?&gt;</td>
    </tr>
    <tr>
        <td align='left' class="td1">Contest</td>
        <td colspan='2' align='left'>
            <select id='contest' name='contest'>
            </select>
        </td>
    </tr>

and my javascript for fetching 2nd list from DB:
Code:
[removed]
&lt;!--
      function getHTTPObject(){
          if (window.ActiveXObject)
              return new ActiveXObject("Microsoft.XMLHTTP");
          else if (window.XMLHttpRequest)
              return new XMLHttpRequest();
          else {
              alert("Your browser does not support AJAX.");
              return null;
          }
      }

    function doWork(storeid){
        httpObject = getHTTPObject();
        if (httpObject != null) {
            httpObject.open("POST", "&lt;?=base_url()?&gt;index.php/smscontest/searchcontest/"+storeid, true);
            httpObject.send(null);
            httpObject.onreadystatechange = setOutput;
        }
    }
    
    function setOutput(){
        if(httpObject.readyState == 4){
            alert(httpObject.responseText);
            document.getElementById('outputText').value = httpObject.responseText;
        }
    }

--&gt;
[removed]

The PHP codes which query the db:
Code:
function searchcontest ($storeid) {
        $query = $this->configmodel->get_contest($storeid);
        $output = Array('contest');
        foreach ($query->result() as $row){
           $arr = Array('contest' => $row->contest);
           $output[] = $arr;
        }

         echo $output;
    }

my problem is the returned $output is an array, how do I pass its value to the "contest" drop down list? (Only one column of the output needs to be populated to the "contest" drop down list.)


Messages In This Thread
Populate query result to drop down list with Ajax - by El Forum - 07-02-2008, 12:42 AM
Populate query result to drop down list with Ajax - by El Forum - 07-02-2008, 01:59 AM
Populate query result to drop down list with Ajax - by El Forum - 07-02-2008, 07:27 PM
Populate query result to drop down list with Ajax - by El Forum - 07-02-2008, 11:31 PM
Populate query result to drop down list with Ajax - by El Forum - 07-03-2008, 12:05 AM
Populate query result to drop down list with Ajax - by El Forum - 07-03-2008, 01:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB