Php 在jQuery AJAX表单提交上使用数据库查询更新表
这是我所拥有的Php 在jQuery AJAX表单提交上使用数据库查询更新表,php,jquery,mysql,ajax,Php,Jquery,Mysql,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},
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);
}
})
}
函数checkDB(代码,用户ID)
{
$(“#notice_div”).html('Loading..);
$.ajax({
类型:“POST”,
url:“/profile/check_code.php”,
数据:{code:code,userid:userid},
//数据:'code='+code+'&userid='+userid,
数据类型:“html”,
成功:功能(结果){
如果(结果==0)
{
$(“#success”).html(代码+”已兑换!';
//警报('success');//测试目的
}
否则如果(结果==2)
{
$('#err').html(代码+'已存在并已被兑换……);
//警报('fail');//测试目的
}else if(结果==1){
$('#err').html(代码+'兑换代码不存在');
}
$(“#注意_div”).html(“”);
//$('#originalquery').hide();
//$('#mainquery').fadeIn('slow');
//$(“#originalquery”)。替换为($(“#mainquery',$(html));
//警报(结果);
}
})
}
这与以下内容在同一页上:
<?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>
因此,基本上您希望根据$user\u id
从数据库中提取记录,并且生成的表需要用ajax显示在请求页面上?对吧?
显示新添加的信息
添加到哪里?您的html div看起来像什么?成功提交后会发生什么?我想你已经确认你的代码被调用了?Javascript应该能够在不刷新页面的情况下更新div。我添加了您提交的HTML表单。提交时,只有两个div根据页面的结果给出消息。我想要自动更新的div根本不涉及ajac。。。这只是同一页上的一个问题是的。到目前为止一切正常,我只需手动刷新页面即可查看新添加的值,新记录将添加到何处?因此,在这种情况下,您必须将php页面上生成的表作为响应返回到调用页面。执行if-else条件检查,如果它不在0到2之间(这意味着您已经在php页面上成功生成了表),则使用jQuery和append
或inserAfter
某些块对DOM进行操作。新记录将直接进入数据库。。。没有真正显示在任何地方,因为我希望它只显示在客户端页面上的原始查询中。我想我明白你的意思。。。基本上,在服务器端页面(当前记录正在更新的地方)中,我回显一个新的表行,并将其返回到客户端页面的结果中,然后使用jquery将其添加到显示表的末尾?这就是你的意思吗?是的!但是你在这里发布的代码没有添加任何记录。我已经在帖子中添加了表。我基本上告诉success函数在末尾将结果添加为html,并将其添加到div id newCode中。我想这就是你的意思是吗?如果是这样的话,我就把它摆弄一下。。。我相信我能从中得到成功:)
<?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>