Php qu和jQuery的数据检索问题
我正在构建一个具有前瞻性输入框的表单(使用)。所选择的输入(在本例中为bands)通过Ajax发送到数据库,然后显示在原告、被告或其他bin的输入框下方 对于90%的情况,它工作得很好,是异步的。当乐队中碰巧有引号或撇号时,问题就出现了。在我的PHP表单处理程序中,我使用Php qu和jQuery的数据检索问题,php,jquery,mysql,ajax,escaping,Php,Jquery,Mysql,Ajax,Escaping,我正在构建一个具有前瞻性输入框的表单(使用)。所选择的输入(在本例中为bands)通过Ajax发送到数据库,然后显示在原告、被告或其他bin的输入框下方 对于90%的情况,它工作得很好,是异步的。当乐队中碰巧有引号或撇号时,问题就出现了。在我的PHP表单处理程序中,我使用mysql\u real\u escape\u string对字符进行转义,但问题似乎是在数据被Ajax处理完毕时产生的 description=Watts, Michael "5000" becomes description
mysql\u real\u escape\u string
对字符进行转义,但问题似乎是在数据被Ajax处理完毕时产生的
description=Watts, Michael "5000" becomes description=Watts%2C+Michael+%225000%22.
这是我的jQuery代码(没什么特别的,只是序列化)和PHP代码。请注意,如果我在我的PHP文件中手动输入description=Watts,Michael“5000”
,它就可以很好地工作(如中所示,它可以抓取否则无法抓取的数据)
jQuery代码:
$('#party_add').live('click', function(){
//grab the form
var thisform=$(this).parents('form');
//serialize the data
var toSend=thisform.serialize();
//add the caseID we stored in #caseId
var storedCaseId=$('#caseId').text();
toSend=toSend+'&caseId='+storedCaseId;
$.ajax({
type : "POST",
url : 'cases_form_handler.php',
data : toSend,
dataType:'json',
success : function(data){
alert(toSend);
//conjure the relevant parties into their respective places in the table below with some sexy json action
$('.party').empty();
for(var x=0; x < data.length; x++){
if(data[x].partyType==1){
$('.party:first').append('<span class=party_addition id='+data[x].partyId+'>'+data[x].description+'</span><br/>');
}else{
//if defendant, put them in the defendant box
if(data[x].partyType==2){
$('.party:first').next('.party').append('<span class=party_addition id='+data[x].partyId+'>'+data[x].description+'</span><br/>');
}else{
//if other, put them in the other box
if(data[x].partyType==3){
$('.party:first').next('.party').next('.party').append('<span class=party_addition id='+data[x].partyId+'>'+data[x].description+'</span><br/>');
}
}
}
}
}
});
更新:
下面是我的php的json部分
$sql = "SELECT varParties.*,relCasesParties.partyType,relCasesParties.active FROM varParties INNER JOIN relCasesParties ON relCasesParties.partyId = varParties.id WHERE relCasesParties.caseId = '$caseId' AND relCasesParties.active='1'";
//build array of results
$query=mysql_query($sql);
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$parties[$x] = array('description'=>$row['description'],'partyId'=>$row['id'],'partyType'=>$row['partyType']);
}
//send it back
echo json_encode($parties);
$sql=“SELECT varParties.*,relcasespatties.partyType,relcasespatties.active从varParties内部连接relcasespatties ON relcasespatties.partyId=varParties.id,其中relcasespatties.caseId='$caseId'和relcasespatties.active='1';
//构建结果数组
$query=mysql\u查询($sql);
对于($x=0,$numrows=mysql_num_行($query);$x<$numrows;$x++){
$row=mysql\u fetch\u assoc($query);
$parties[$x]=数组('description'=>$row['description'],'partyId'=>$row['id'],'partyType'=>$row['partyType']);
}
//寄回去
echo json_编码($parties);
在哪里使用htmlentities?使用
$description=mysql_real_escape_string(urldecode($_POST['description']));
现在,当您试图输出JSON字符串时,答案是不同的
对于循环使用:
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$parties[$x] = array('description'=>addcslashes($row['description'],'"'),'partyId'=>$row['id'],'partyType'=>$row['partyType']);
}
for($x=0,$numrows=mysql_num_行($query);$x<$numrows;$x++){
$row=mysql\u fetch\u assoc($query);
$parties[$x]=数组('description'=>addcslashes($row['description'],““”),'partyId'=>$row['id'],'partyType'=>$row['partyType']);
}
hmmm。它在正确清理数据方面发挥了神奇的作用,但由于某些原因,它无法在之后找到并插入到框中;它说它们的id为0。我的引号/单引号是否弄乱了事情?每当我直接从php文件运行它时,它似乎能够正确返回内容…应用HTMLentie在phpI中输出字符串的s()更新了一些代码,因为我仍然无法让它工作,并且我很难找到在哪里使用HTMLentites。好的。我从$description声明中删除了urldecode,并将循环替换为您创建的循环。它现在可以用于“C+C音乐工厂”之类的东西但仍然不接受像“卡姆隆”、“枪与玫瑰”或“瓦茨,迈克尔”5000之类的东西。谢谢你的帮助,如果我有点粗暴地对待这个解决方案,我深表歉意。几乎让我想打一个小孩。我不久前发现了一个脚本,用于处理应用addslashes的服务器之间的差异。我加入了for循环,现在它可以工作了。非常感谢你在这件事上帮了我的忙。
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$parties[$x] = array('description'=>addcslashes($row['description'],'"'),'partyId'=>$row['id'],'partyType'=>$row['partyType']);
}