Jump to content

Good Dynamic Dropdown List but need ARRAY...


siwelis

Recommended Posts

Here's the JS code:

 

<script type="text/javascript">
function AjaxFunction(cust_id)
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                    {
                 httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
            catch (e)
                    {
                try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
             }
                catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
            }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {

var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.testform.subcat.options.length-1;j>=0;j--)
{
document.testform.subcat.remove(j);
}


for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);

} 
      }
    }
    var url="dd.php";
url=url+"?cust_id="+cust_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>

 

And I know what I need to modify is this section so that the optn.value will be different:

var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);

 

The Javascript code pulls data from this php code:

$q=mysql_query("select * from Customers where cust_type='$cust_id'");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[cust_name]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";

 

I'm pretty sure what I need to modify is the while portion, but I'm not sure how to do it. Where it says $nt[cust_name] it also needs to have the second value needed something like $nt[cust_number]

 

Anyone have any advice on this issue?

 

Thank you very much and happy Valentines day!

 

This code originated from: http://www.plus2net.com/php_tutorial/php_drop_down_list.php

[ RESOLVED WITH THIS FIX ]

 

In the JavaScript portion mentioned in the previous post, I changed

 

for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i][0];
optn.value = myarray[i][1];
document.testform.subcat.options.add(optn);
}

 

That was the hardest part. I had to learn about Javascript multi-dimensional / nested arrays. My helpers were Google and these two explanations of this array type:

http://jennifermadden.com/javascript/arraySimple2.html

http://www.elated.com/articles/nested-arrays-in-javascript/

 

 

In the PHP portion mentioned in the previous post, first I changed the syntax to something I prefer working with (using single quotes instead of quotes), then realizing JavaScript was using this data in the form, I formatted this data into a JavaScript array...

 

while($nt=mysql_fetch_array($q)){
$str=$str.'new Array("'.$nt[cust_name].'","'.$nt[arrayvaluefromMYSQL_2].'"),';
}

 

Hopefully this will help someone out!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.