Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Consulta de valores suspensos Onchange resp. tabelas - com php, mysql, ajax, jquery


user ajax onchange select box passa o valor de select

HTML
 <select name="sometest" onchange="javascript:call_ajax_fun(this.value);">
      <option value="1"> A </option>
      <option value="2"> B </option>
      <option value="3"> C </option>
      <option value="4"> D </option>
      <option value="5"> E </option>
    </select>

Função JS Ajax
function call_ajax_fun(str)
{

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            var result = xmlhttp.responseText;
            if(xmlhttp.responseText!='')
            {
                document.getElementById('your_result_div_id').innerHTML =result ;
                            // put your result in your div 
            }   
        }
    }



    var url="get_result.php?pas_val="str;

    xmlhttp.open("GET",url,true);

    xmlhttp.send();
}

e seu arquivo de resultado será o seguinte

get_result.php
<?php
if(isset($_REQUEST['pas_val']))
{
   $pas_val = $_REQUEST['pas_val'];
   if($pas_val<5)
   {
     $tbl = "table_1";
   }
   else
  {
     $tbl = "table_2";
  }


// your table is in $tbl variable
//   your your table here in your code


}
else
{
  exit;
}
?>