如何在下拉列表中的第二个变量中存储值

时间:2017-06-22 10:25:08

标签: php mysql ajax

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

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

take a look on the image

<?php

            // code for submit button action
            global $wpdb, $site_name, $data;
        //variables that handle the retrieved data from mysql database based on the ID of the variable in HTML (select)



        if(isset($_POST['query_submit']))
        {


           if(isset($_POST['site_name'])) 
              { 
               $site_name=$_POST['site_name'];

              }
              else { $site_name=""; }
    var_dump($site_name);

/ *上面的行添加了
      var_dump在$ site_name变量中显示3个值是否有一种方法可以在SQL查询结束之前修剪或提取第三个值。  * /

$sql = $wpdb->prepare("select i.siteID
             , i.siteNAME
             , i.equipmentTYPE
             , c.latitude
             , c.longitude
             , c.height 
             , o.ownerNAME
             , o.ownerCONTACT
             , x.companyNAME
             , y.subcontractorCOMPANY
             , y.subcontractorNAME
             , y.subcontractorCONTACT
          from site_info i
          LEFT  
          JOIN owner_info o
            on i.ownerID = o.ownerID
          LEFT  
          JOIN company_info x
            on i.companyID = x.companyID
          LEFT 
          JOIN subcontractor_info y
            on i.subcontractorID = y.subcontractorID
            LEFT JOIN site_coordinates2 c
            on i.siteID=c.siteID 
            where 
            i.siteNAME = %s
            AND 
            o.ownerNAME = %s
            AND 
            x.companyNAME = %s
           ",$site_name,$owner_name,$company_name);

         $query_submit =$wpdb->get_results($sql, OBJECT);

  <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();




?>

2 个答案:

答案 0 :(得分:0)

这个怎么样:

PHP 脚本中,您可以explode将siteName加入arrayPHP explode() documentation

if(isset($_POST['site_name'])) 
   { 
       $site_name=$_POST['site_name'];
       $values = explode(",", $site_name);
       $site_name = $values[0]; //Or whatever cell you want
   }
 else { $site_name=""; }

E D I T:

正如我之前所说:

在PHP中,更改此内容:

echo "<option value = '".$row ->ownerID.",".$row ->companyID.",".$row ->siteNAME."'>".$row->siteNAME."</option>";

收件人:

echo "<option id = '".$row ->ownerID."' name = '".$row ->companyID."' value = '".$row ->siteNAME."'>".$row->siteNAME."</option>";

在ajax中,更改此内容:

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

收件人:

$('#site_name').change(function(){
   var ownerID = $(this).children(":selected").attr("id");
   var companyID = $(this).children(":selected").attr("name");

使用此代码,您不需要我在编辑之前提供的代码,您需要

更改此内容:

if(isset($_POST['site_name'])) 
   { 
       $site_name=$_POST['site_name'];
       $values = explode(",", $site_name);
       $site_name = $values[0]; //Or whatever cell you want
   }
 else { $site_name=""; }

收件人:

if(isset($_POST['site_name'])) 
   { 
       $site_name=$_POST['site_name']; //It'll contain just what you want ;)
   }
 else { $site_name=""; }

答案 1 :(得分:-1)

$(&#39;#OWNER_NAME&#39)。HTML(数据[0] .model_no); 像这样使用model_no是数据库字段名使用。