2016-02-01 85 views
-1

我有一個代碼,其中當我選擇通過下拉值,我選擇的名稱應通過一個PHP變量$ CH所以第二個下拉它運行一個mysql查詢。通過使用Javascript/jQuery的/ AJAX傳遞選擇值到PHP varibale

這裏是我的第一個下拉代碼:

<select name="cha_name" id="cha_name" onChange="fillcha(this.value);"> 
     <option value="0">--SELECT--</option> 
     <?php 
      $res = mysqli_query($con, "select customer_code, customer from master_customer where is_cha = 1 order by customer") or die(mysqli_error($con)); 
      while($row = mysqli_fetch_array($res)){ 
       echo "<option value=\"".$row[1]."$$".$row[0]."\" ".(($row[1]==$wo_order[1])?"selected='selected'":"").">".$row[1]."</option>"; 
      } 
     ?> 

     </select> 

當我選擇這個,值應該是在一個PHP變量$ CH捕捉無需重新加載頁面顯示的PHP變量可以進一步在MySQL中使用查詢。請幫忙,因爲我仍然處於JQuery和Ajax的學習階段。

MySQL查詢如下:

SELECT container_no FROM `cex_work_order_det` WHERE `cha_name`='$cha' 
+0

的http:// stackoverflow.com/a/5861207/4229270 – Sinto

+0

試試這個:SELECT container_no FROM'cex_work_or der_det' WHERE'cha_name' =「$茶」 – Monty

回答

1

爲您必須使用jQuery和AJAX jQuery代碼

<script> 
    $("document").ready(function() 
    { 
    $("select[name='cha_name']").change(function() // function sorting Domain according to server name 
    { 
     var customer_code=$("select[name='cha_name']").val(); 
     $.get('ajax_actions.php?act=your-act-name&customer_code='+customer_code,function(data) 
     { 
      alert(data); 
     }); 
    }); 
    }); 
</script> 

ajax_actions.php

<?php 
error_reporting(0); 
foreach ($_GET as $name => $value) { $$name = $value; } 
foreach ($_POST as $name => $value) { $$name = $value; } 

// warning Dont chnage anything in This ajax action file 
// ------------------------------------------------------------------------------- 
if($act=="your-act-name") // do your actions 

{ 
    // fire here your query and echo you result here 
} 
+0

了投票,如果你的問題是sloved – Raj

+0

沒有動作的意思是我應該寫我的MySQL查詢? – Amlan

+0

雅火查詢和回聲查詢結果,如果查詢returs多個記錄,然後使用JSON_encode – Raj