【问题标题】:Data retrieval problem with qu and jQueryqu 和 jQuery 的数据检索问题
【发布时间】:2011-08-04 17:11:12
【问题描述】:

我正在构建一个具有前瞻输入框的表单(使用jQuery UI)。选择的输入(在本例中为波段)通过 Ajax 发送到数据库,然后显示在原告、被告或其他 bin 中的输入框下方。

对于 90% 的情况,它工作得很好,异步的光彩照人。当乐队恰好有引号或撇号时,就会出现问题。我在我的 PHP 表单处理程序中使用 mysql_real_escape_string 转义字符,但问题似乎源于数据在 Ajax 结束时。

description=Watts, Michael "5000" becomes description=Watts%2C+Michael+%225000%22.

这是我的 jQuery 代码(没什么花哨的,只是序列化)和我的 PHP 代码。请注意,如果我手动将description=Watts, Michael "5000" 输入到我的 PHP 文件中,它就可以正常工作(例如,它可以获取原本无法获取的数据)。

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 代码和失败的 SQL 调用:

    $description=mysql_real_escape_string($_POST['description']);
    $sql="SELECT id FROM varParties WHERE description='$description'";
    $result=mysql_query($sql);

    while($row=mysql_fetch_assoc($result)){
        $partyId=$row['id'];
    }

更新:

这是我的 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);

我会在哪里使用 htmlentities?

【问题讨论】:

    标签: php jquery mysql ajax escaping


    【解决方案1】:

    使用

    $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']);  
            }
    

    【讨论】:

    • 嗯。它在正确清理数据方面创造了奇迹,但由于某种原因,它无法找到并将它们插入到盒子中;它是说他们的 id 为 0。我的引号/单引号是否搞砸了?每当我直接从 php 文件运行它时,它似乎能够正确返回东西......
    • 应用 htmlentities() 在 php 中输出字符串
    • 我更新了一些代码,因为我仍然无法让它工作,并且很难找出在哪里打 htmlentitites。
    • 好的。我从 $description 声明中删除了 urldecode,并将循环替换为您创建的循环。它现在适用于诸如“C+C 音乐工厂”之类的东西,但仍然不适用于诸如“Cam'ron”、“Guns n' Roses”或“Watts, Michael “5000”之类的东西。感谢所有的帮助,如果我对解决方案有点粗暴,请道歉 D:
    • 想通了。简直让我想打一个小孩。我不久前发现了一个脚本来处理服务器应用添加斜杠的差异。我包括在内,并且通过您的 for 循环,它现在可以工作了。感谢您在这方面帮助我。
    猜你喜欢
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2019-02-13
    相关资源
    最近更新 更多