2013-04-29 188 views
0

我想通過jQuery和AJAX將文本框的值(發票號)發送到PHP文件。從PHP文件中,我返回JSON編碼數據。我想在下面的文本框(總金額,餘額)和選擇框(支付模式,付款狀態)中顯示從數據庫中提取的值。通過jQuery和PHP加載JSON數據

我沒有得到我希望的輸出。我哪裏錯了?

HTML:

<form name="inv_payment" method="post" action="" style="margin:0px;width:400px;"> 
    <fieldset> 
     <legend>Payment details</legend> 
     <label for=inv_num>Invoice Number</label> 
      <input id=inv_num name=inv_num type=number placeholder="12345" required autofocus > 
      <input type=button name="get_details" id="get_details" value="Get Details" > 
      <label for=tot_amt>Total Amount</label> 
      <input id=tot_amt name=tot_amt type=text placeholder="12345" disabled > <br/> 
      <label for=pay_up>Pay Up </label> 
      <input id=pay_up name=pay_up type=text placeholder="12345" required ><br/> 
      <label for=bal_amt>Balance Amount&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label> 
      <input id=bal_amt name=bal_amt type=text placeholder="12345" disabled > <br/> 
       <label id="paymt_mode"><strong>Payment Mode</strong></label> 
          <select name="pay_mode" style="width: 130px"> 
          <option selected="selected">Cash</option> 
          <option>Cheque</option> 
          </select><br/> 
       <label id="paymt_status"><strong>Payment Status</strong> </label> 
          <select name="pay_status" style="width: 130px"> 
          <option selected="selected">Pending</option> 
          <option>Completed</option> 
          </select><br/> 
      <input type="submit" value="Payment" name="pay_btn"> 
     </fieldset> 
</form> 

的jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script type="text/javascript"> 

    $(document).ready(function() { 


     $('#get_details').click(function() { 

     //To pass the invoice number and fetch related details 
     $.getJSON("get_invpay_details.php", {id: $('#inv_num').val()}, function(data){ 
      $('#tot_amt').val(data['Total_Amount']); 
      $('#bal_amt').val(data['Balance_Amount']); 
      $('#pay_mode').val(data['Payment_Type']); 
         $('#pay_status').val(data['Payment_Status']); 
     }); 
       }); 
</script> 

PHP:

<?php 
     include('includes/dbfunctions.php'); 
     include('includes/db.php'); 
     if(isset($_GET['id'])) 
    { 
       $tmp = $_GET['id']; 
     $sql = mysql_query("select Total_Amount,Balance_Amount,Payment_Type,Payment_Status from invoice where InvoiceNum='$tmp'"); 
     if(mysql_num_rows($sql)) 
     { 
      $data = mysql_fetch_array($sql); 
      echo json_encode($data); 
     } 
    } 

?> 
+0

如果它不是你希望的輸出,你到底是什麼?另外,考慮轉移到mysqli或PDO,你的查詢看起來很奇怪! – adeneo 2013-04-29 06:38:08

+0

告訴我們你的json響應...檢查你的控制檯.. – bipen 2013-04-29 06:39:50

+0

你可以顯示你的'json_encode($ data)'內容.. – Ranjith 2013-04-29 06:41:20

回答

0

您的JSON響應值填充object.data是這樣的。

$('#tot_amt').val(data.Total_Amount); 
$('#bal_amt').val(data.Balance_Amount); 
$('#pay_mode').val(data.Payment_Type); 
$('#pay_status').val(data.Payment_Status); 

這裏dataobject不是array

+0

感謝ranjith的回覆。我嘗試了你的代碼,但仍然沒有工作。 – Rams 2013-04-29 07:05:16

0

我檢查了我的查詢,它在mysql編輯器中給了我正確的輸出。

我所能看到的是..你有你的問題一個錯字...

失蹤});爲document.ready..which可能會打破你的腳本..(這應該有錯誤的控制檯雖然..)..無論如何要檢查的東西..

$(document).ready(function() { 


    $('#get_details').click(function() { 

    //To pass the invoice number and fetch related details 
     $.getJSON("get_invpay_details.php", {id: $('#inv_num').val()}, function(data){ 
      $('#tot_amt').val(data['Total_Amount']); 
      $('#bal_amt').val(data['Balance_Amount']); 
      $('#pay_mode').val(data['Payment_Type']); 
      $('#pay_status').val(data['Payment_Status']); 
     }); 
    }); 
    }); 
    //^^--here 
+0

謝謝bipen。我忘記關閉$(document).ready(function(){with});現在值顯示在我的文本框(總金額)中,但我的選擇框(付款方式和付款狀態沒有反映出來)。 – Rams 2013-04-29 07:19:14

+0

這應該工作......確保你在你的選擇框中有'data ['Payment_Type']'選項 – bipen 2013-04-29 07:24:17