Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Update dropdown with ajax and php

    Hi guys, I have this code which I found somewhere and I got it working almost perfectly.
    Every time someones selects something from the dropdown, it fetches the value from a phpfile, and updates a second dropdown.
    I guess it is the standard way of doing this kind of task.

    Code:
    <script type="text/javascript">
    function AjaxFunction(cat_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.subtipo.options.length-1;j>=0;j--)
    {
    document.testform.subtipo.remove(j);
    }
    
    
    for (i=0;i<myarray.length;i++)
    {
    var optn = document.createElement("OPTION");
    optn.text = myarray[i];
    optn.value = myarray[i];
    document.testform.subtipo.options.add(optn);
    
    } 
          }
        }
    	var url="actividades_check.php";
    url=url+"?cat_id="+cat_id;
    url=url+"&sid="+Math.random();
    httpxml.onreadystatechange=stateck;
    httpxml.open("GET",url,true);
    httpxml.send(null);
      }
    </script>
    PHP Code:
    <?
    $cat_id
    =$_GET['cat_id'];

    include_once 
    'includes/functions.inc.php';


    if (!
    $sql $db->sql_query("SELECT * FROM categorias WHERE nome = '" $cat_id "'"))
        {
            
    message_die("DS"TRUE);
        }
    $linha $db->sql_fetchrow($sql);


    $q $db->sql_query("select * from subcategorias where id_categoria=" $linha['id']);
    echo 
    mysql_error();
    $myarray=array();
    $str="";
    while(
    $nt=$db->sql_fetchrow($q)){
        
    $nt['nome'] = utf8_encode($nt['nome']);
    $str$str "\"$nt[nome]\"".",";
    #$str=$str . "\"$cat_id\"".",";
    }
    $str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
    echo "new Array($str)";

    ?>
    Right now this code allows me to pass the $nt['nome'] and update the dropdown with something like this:
    <option value="$nt['nome']">$nt['nome']</option>

    And I'm looking to get:
    <option value="$nt['id']">$nt['nome']</option>

    How can I get the ID value?

    Thanks a lot guys

  • #2
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •