2012-10-06 15 views
0

我想使用jQuery在invoice.php文件上傳遞選定的值。在invoice.php文件上發送值之後。我想在index.php頁面選擇的輸入框中顯示invoice.php文件的echo結果。就像一個例子:如果有人選擇項目,然後在速率輸入框中顯示速率。這裏是我的代碼和圖像enter image description here如何通過jQuery傳遞選定的值

index.php文件代碼:

<html> 
    <head> 
     <title>Invoice</title> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 

    $(document).ready(function(){ 
    $(".item").change(function(){ 
     calculate(); 
    }); 
}); 

function calculate() { 

var item = []; 
    $(".item").each(function() { 
      var num=(this.value); 
      item.push(num); 
    }); 


    $('input[name=rate]').each(function(i){ 
     $.get(
      '/invoice_final_2.php', 
      {'product':item[i]}, 
      function(data) { 
       $(this).html(data); 
      }); 
    }); 
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
</script> 
<style type="text/css"> 
table 
{ 
    border: 1px solid black; 
    border-bottom: none; 
    width: 300px; 
} 


td 
{ 
    border-left: 1px solid black; 
    border-bottom: 1px solid black; 
    padding:5px 5px 5px 5px; 
} 
#first 
{ 
    border-left: none; 
    border-bottom: 1px solid black; 
} 
</style> 
    </head> 
    <body> 
     <form method='post' name='form1' action='welcome.php'> 
     <table cellpadding="0" cellspacing="0"> 
      <tr> 
       <td id="first">Item</td> 
       <td>Rate</td> 
      </tr> 
      <?php 
      $num=4; 
      for ($i=0; $i<$num; $i++) 
      { 
       echo " 
       <tr> 
       <td id='first'> 
       <select class='item' name='item'> 
       <option>Select one</option> 
       <option>Deluxe Plan</option> 
       <option>Premium Plan</option> 
       <option>Standard Plan</option> 
       <option>Economy Plan</option> 
       </select> 
       </td> 
       <td><input class='rate' type='text' name='rate'/></td> 
       </tr>"; 
      } 
      ?> 
    </table> 
</form> 
</body> 
</html> 

invoice.php文件代碼:

<?php 
include "dbconnect.php"; 

$product=$_GET['product']; 

$query="select * from item where item_name='$product'"; 
$result=mysql_query or die (mysql_error()); 
$row=mysql_fetch_assoc($result); 
$rate=$row['rate']; 

echo "$rate"; 

?> 

回答

0

我不知道你想達到什麼目的,但您不需要使用each並使用索引逐個發送值,您可以發送一次

function calculate() { 
    var items = $(".item").map(function() { 
      return this.value; 
    }).get(); 

    $.get('/invoice_final_2.php', 
     {'product': items }, 
     function(data) { 
      // 
     } 
    ); 
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 
+0

如果我使用數組,那麼如何在index.php文件中顯示這個數組選擇輸入框。你能解釋一下嗎? – user1493448

+0

@ user1493448然後您應該遍歷數組並讀取值,我會序列化結果,這樣您可以更輕鬆地處理數據。 – undefined

+0

你可以給我一個序列化的例子代碼。因爲我是jquery的新手。你可以給我代碼嗎? – user1493448