2012-02-17 23 views
0

這是我有...使用jQuery AJAX形式的數據庫查詢更新表提交

function checkDB(code, userid) 
    { 
     $("#notice_div").html('Loading..'); 
     $.ajax({ 
     type: "POST", 
     url: "<?php bloginfo('template_url'); ?>/profile/check_code.php", 
     data: { code: code, userid: userid}, 
     //data: 'code='+code+'&userid='+userid, 
     datatype: "html", 
     success: function(result){ 

      if(result == 0) 
      { 
       $('#success').html(code + ' has been redeemed!'); 
       // alert('success');//testing purposes 
      } 
      else if(result == 2) 
      { 
       $('#err').html( code + ' already exists and has already been redeemed....'); 
       //alert('fail');//testing purposes 
      }else if(result == 1){ 
       $('#err').html( code + ' redeem code doesnt exist');  
      } 
      $("#notice_div").html(''); 
      //$('#originalquery').hide(); 
      //$('#mainquery').fadeIn('slow'); 
      //$("#originalquery").replaceWith($('#mainquery', $(html))); 

      //alert(result);   
      } 
     }) 

    } 

這是在同一個頁面,因爲這於:

<?php 

$sql="SELECT * FROM wp_scloyalty WHERE userid = '$user_id'"; 
$result=mysql_query($sql); 
?> 
<table id="poo" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0"> 
<tr> 
    <td><strong>Product</strong></td> 
    <td><strong>Code</strong></td> 
    <td><strong>Value</strong></td> 
</tr> 
<?php while($rows=mysql_fetch_array($result)){ ?> 
<tr> 
    <td><? echo $rows['product']; ?></td> 
    <td><? echo $rows['code']; ?></td> 
    <td><? echo $rows['value']; ?></td> 
</tr> 
<? } ?> 
</table> 


      <form method="post" class="sc_ajaxxx" id="sc_add_voucherx" name="sc_ajax" action="" onsubmit="checkDB(document.sc_ajax.sc_voucher_code.value, <?php echo $user_id; ?>); return false;"> 

      <button id="submit-code" type="submit" >Submit</button> 

     </form> 

這是HTML .. 。

當提交ajax請求時,我希望表格自動顯示新添加的信息。我知道爲什麼它不工作,但不知道如何讓它刷新。我認爲刷新div並不是真的可能,因爲我希望它是..我想創造一個新的查詢,並把它帶入成功功能?

謝謝:)

表查詢

編輯:

<?php 

$sql="SELECT * FROM wp_scloyalty WHERE userid = '$user_id'"; 
$result=mysql_query($sql); 
?> 
<table id="poo" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0"> 
<tr> 
    <td><strong>Product</strong></td> 
    <td><strong>Code</strong></td> 
    <td><strong>Value</strong></td> 
</tr> 
<?php while($rows=mysql_fetch_array($result)){ ?> 
<tr> 
    <td><? echo $rows['product']; ?></td> 
    <td><? echo $rows['code']; ?></td> 
    <td><? echo $rows['value']; ?></td> 
</tr> 
<? } ?> 
<div id="newCode"></div> 
</table> 
+0

你的html div是什麼樣的?成功提交後會發生什麼?我假設你驗證了你的代碼正在被調用? Javascript應該能夠在不刷新頁面的情況下更新div。 – 2012-02-17 16:57:06

+0

我添加了您提交的HTML表單。在提交時,只有兩個div可以根據頁面上顯示的結果給出信息。我想要自動更新的div並不真正涉及到ajac ......它只是一個查詢在同一頁上.. – JamesG 2012-02-17 17:05:08

回答

1

所以基本上你想拉從數據庫基於$user_id記錄,並將所得產生的表需要上顯示用ajax請求頁面?對?

顯示新添加的信息

哪來它被添加反正

+0

是的,這就是對的。這一切都工作到目前爲止,我只需刷新頁面手動查看新增值 – JamesG 2012-02-17 16:58:20

+0

和新記錄被添加到哪裏? 所以在這種情況下,您必須將在php頁面上生成的表格回顯爲調用頁面作爲響應。如果它不在0到2之間(這意味着你已經在php頁面上成功生成了表),那麼執行if if條件檢查,使用jQuery和'append'或'inserAfter'操作DOM。 – SachinGutte 2012-02-17 17:02:27

+0

新記錄直接進入數據庫...並不真正顯示在任何地方,因爲我希望它會在客戶端頁面上顯示原始查詢。我想我明白你說的是什麼......基本上在服務器端頁面(當前記錄正在更新的地方),我回應一個新的表格行,並將結果帶回到客戶端頁面,然後添加到jQuery顯示錶的結尾?你是這個意思嗎? – JamesG 2012-02-17 17:38:17