因此,在此桌子上,我一直将头撞在桌子上几个小时,而且我什么都没走,因此我们将不胜感激。
下面的代码有两个jquery事件处理程序,它们会触发ajax请求。第一个使用GET,并且从服务器返回的数据是JSON编码的-它工作正常。返回的第二个(“ button#addTx”)使Firebug产生此错误:
未捕获的异常:[异常…“提示被用户终止” nsresult:“ 0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“ JS框架::资源://gre/components/nsPrompter.js :: openTabPrompt ::行468”数据:没有] 0行
未捕获的异常:[异常…“提示被用户终止” nsresult:“ 0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“ JS框架::资源://gre/components/nsPrompter.js :: openTabPrompt ::行468”数据:没有]
0行
这根本没有帮助。服务器端脚本正在将原始html打印到屏幕上,目的是使用jquery html replace来更新到发起请求的页面。随着数据库更新,数据已正确发布,但除此之外我不知道。我已将其重写为尝试GET仍会产生相同的错误:-(
帮助会很棒-西蒙,谢谢
$(document).ready(function(){ $("button.delete").click(function(){ var txid = this.id; var amountID = "#amount" + txid; var amount = $(amountID).html(); // <![CDATA[ var url = "delete.php?txid=" + txid + "&am=" + amount; $.ajax({ type: "GET", url: url, success: function(msg){ txid = "ul#" + txid; $(txid).hide(); var values = msg; var e = "#" + values.category + "AmountLeft"; var a = values.amount; $(e).html(a); } }); }); $("button#addTx").click(function(){ // <![CDATA[ var url = "addTran.php"; //var dataV = var data = "category=" + document.getElementById("category").value + "&what=" + document.getElementById("what").value + "&amount=" + document.getElementById("amount").value + "&date=" + document.getElementById("date").value; $.ajax({ type: "POST", url: "addTran.php", //async: false, data: "category=Groceries&what=Food&amount=2.33&date=2/3/2011", success: function(msg){ $("transList").replaceWith(msg); } }); }); });
这是服务器端脚本
<?php session_start(); include('functions.php'); //if the user has not logged in if(!isLoggedIn()) { header('Location: index.php'); die(); } $category = $_POST['category']; $what = $_POST['what']; $amount = $_POST['amount']; $date = $_POST['date']; $category = mysql_real_escape_string($category); $what = mysql_real_escape_string($what); $amount = mysql_real_escape_string($amount); $date = mysql_real_escape_string($date); $date = convertDate($date); //add trans to db include('dbcon.php'); $query = "INSERT INTO transactions ( category, what, amount, date) VALUES ( '$category','$what','$amount','$date');"; mysql_query($query); //grab the remaining amount from that budget $query = "SELECT amount_left FROM cards WHERE category = '$category';"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); $oldAmountLeft = $row["amount_left"]; //update the amount left $amountLeft = $oldAmountLeft - $amount; mysql_free_result($result); //add new value to db $query = "UPDATE cards SET amount_left = '$amountLeft' WHERE category = '$category';"; mysql_query($query); //generate the list of remaining transactions, print to screen to send back to main page $query = "SELECT txid, what, amount, date FROM transactions WHERE category = ('$category');"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $d = convertDateReverse($row["date"]); $what = $row["what"]; $amount = $row["amount"]; $txid = $row["txid"]; ?> <li><ul class="trans" id="<? echo $txid; ?>"><li class="date"><? echo $d; ?></li><li class="what"><? echo $what; ?></li><li class="amount" id="amount<? echo $txid; ?>"><? echo $amount; ?></li><button class="delete" id="<? echo $txid; ?>">Delete</button><li></li></ul></li> <? } mysql_free_result($result); mysql_close(); header("Content-type: application/x-www-form-urlencoded"); //do I need this? I have a " header("Content-type: application/json"); " in the working one ?>
解决了问题:因此在html标记中,保存数据字段的表单应具有
onsubmit="return false;"
在里面!
感谢所有帮助人员,我已经实现了您的所有建议,并且我的代码现在更小,更易于管理!
干杯
西蒙