2014-11-02 100 views
2

我試圖使兩個字段在從數據庫生成的項目被「選定」後自動填充。我不確定我犯了什麼錯誤。我也使用了螢火蟲,但沒有顯示任何錯誤訊息。從下拉菜單中選擇一個項目後,它不會填充。請幫助我,讓我知道我做錯了什麼。根據用ajax選擇的值填充多個字段

下面是腳本:

<script type="text/javascript" language="javascript"> 
$(function() { 
    $('#description').bind('input', function() { 
     $(this).val() // get value 
     $.ajax({ 
      type: 'POST', 
      url: 'orderAuto.php', 
      data: { 
       url: $('#description').val() 
      }, 
      dataType: 'json', 
      success: function (data) //on recieve of reply 
      { 
       var skuId = data[0]; 
       var unitPrice = data[1]; 
       $('#sku_1').val(skuId); 
       $('#uPrice_1').val(unitPrice); 
      } 
     }); 
    }); 
}); 
</script> 

這裏是我的形式從數據庫字段和部分:

<form name="form" method="get"> 
<table width="70%" border="5" align="center"><tr> 
<th scope="row">Item Name</th> 
<th scope="row">Item SKU</th> 
<th scope="row">Quantity</th> 
<th scope="row">Special Note</th> 
<th scope="row">Unit Price</th> 
<th scope="row">Total Price</th> 
</tr> 
<tr> 
<th scope="row"> 
<?php 
include('connect.php'); 

$result = mysqli_query("SELECT description FROM products") 
      or die(mysqli_error()); 
print '<select name="description" id="description" value="description">'; 
print '<option value="" disabled selected>Please Select A Product</option>'; 
while ($info = mysqli_fetch_array($result)) 
{ 
     $p = $info["description"]; 
     $p = htmlspecialchars($p); 
     printf('<option value="%s">%s</option>', $p, $p); 
} 
print '</select>'; 
?> 
</th> 
<th scope="row"><input name="sku_1" id="sku_1" readonly /></th>  
<th scope="row"><input name="qty_1" /></th> 
<th scope="row"><input name="note_1" /></th> 
<th scope="row"><input name="uPrice_1" id="uPrice_1" readonly /></th> 
<th scope="row"><input name="tPrice_1" readonly /></th> 
</tr> 
</table> 
<input type="submit"/> 
</form>  

這裏是orderAuto.php:

<?php 
    include('connect.php'); 
    $p = $_POST['description']; 
    $result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'"); 
    $array = mysqli_fetch_array($result); 
    echo json_encode($array); 
?> 
+0

'網址:$( '#說明')VAL()'應該是'說明:$( '#說明')VAL()' – 2014-11-02 01:39:46

+0

你不應該使用。改變()而不是.bind()?這也是錯的,試試這個http://api.jquery.com/change/ – Prashank 2014-11-02 01:39:49

+0

@Prashank,我不打算進入,但'.bind()'現在應該是'.on()'。 ''input''可能是正確的,因爲這是一個自動填充框。如果不是,那麼它肯定會是「改變」。 – 2014-11-02 01:44:45

回答

1

更新

<script type="text/javascript" language="javascript"> 
$(function() { 
    $('#description').change(function() { 
     $.ajax({ 
      type: 'POST', 
      url: 'orderAuto.php', 
      data: { 
       description: $(this).val() 
      }, 
      dataType: 'json', 
      success: function (data) //on recieve of reply 
      { 
       var skuId = data[0]; 
       var unitPrice = data[1]; 
       $('#sku_1').val(skuId); 
       $('#uPrice_1').val(unitPrice); 
      } 
     }); 
    }); 
}); 
</script> 

<?php 
    include('connect.php'); 
    $p = mysqli_real_escape_string($_POST['description']); // should be doing this 
    $result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'"); 
    $array = mysqli_fetch_array($result); 
    echo json_encode($array); 
?> 
+0

它的作品!非常感謝。 – Joseph 2014-11-02 02:34:54

相關問題