2012-02-17 46 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