如何在下拉列表中将siteNAME的值保存在单独的变量中

时间:2017-06-22 05:25:46

标签: php mysql ajax select drop-down-menu

我有一个用PHP和MYSQL编写的网站,其中ajax包含3个依赖的drodpdown列表,其中第一个下拉列表包含了另外两个dorpdown列表所需的几个值。

我需要的是还能够将其中一个值存储在第二个变量中,以便在其他查询中单独使用它。

  <td><select id="site_name"  name = "site_name">
  <option value="">Select Site</option>
<?php
 $query_site_name =$wpdb->get_results("select DISTINCT 
    i.siteNAME, 
    i.ownerID, 
    i.companyID,
    o.ownerNAME,
    x.companyNAME
   from site_info i
   LEFT  
   JOIN owner_info o
   on i.ownerID = o.ownerID
   LEFT  
   JOIN company_info x
   on i.companyID = x.companyID
   ");

  foreach($query_site_name as $row)
  {   
     //$result1 = $row->ownerID;
    // $result2 = $row->companyID;  
    echo "<option value = '".$row ->ownerID.",".$row ->companyID.",".$row ->siteNAME."'>".$row->siteNAME."</option>";
   // echo "<option value = '".$row ->siteNAME."'>".$row->siteNAME."</option>";         
  } 
?>
</select></td>

ajax代码

 <script type="text/javascript">

// make Dropdownlist depend on each other
$(document).ready(function(){


 // depend owner name on site name  

   $('#site_name').change(function(){
     var arrayId = $(this).val().split(",");
     if(arrayId != ""){
       var ownerID = arrayId[0];    //0
       var companyID = arrayId[1];  //1

     $.ajax({
      url:"<?php echo get_stylesheet_directory_uri(); ?>/dropdown_fetch_owner.php",
       method:"POST",
       data:{ownerID:ownerID,companyID:companyID},
       dataType:"text",
       success:function(data){
                 var Response = data.split("--");
                 $('#owner_name').html(Response[2]);
                 $('#Company_name').html(Response[4]);
        }
     });


   }


  });


});

</script>

dropdown_fetch_owner.php

<?php

 include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php');
 global $wpdb,$owner_name,$company_name;

   $sql =$wpdb->get_results("select ownerID, ownerNAME from owner_info where ownerID = '".$_POST['ownerID']."' ORDER BY ownerNAME");
   $owner_name = '--Owner--';
   var_dump($sql);
  foreach($sql as $row){


      $owner_name.= "<option value ='".$row ->ownerID."'>".$row->ownerNAME."</option>";
  }
  echo $owner_name;


   $sql =$wpdb->get_results("select companyID, companyNAME from company_info where companyID = '".$_POST['companyID']."' ORDER BY companyNAME");
   $company_name = '--Company--';
   var_dump($sql);


   foreach($sql as $row){

     $company_name.= "<option value ='".$row ->companyID."'>".$row->companyNAME."</option>";
   }
   echo $company_name;
   exit();




?>

0 个答案:

没有答案