douqudi5282 2014-05-07 10:32
浏览 44
已采纳

在通过ajax Json格式提交时在mysql中插入查询问题

I am developing one website where i insert data through ajax in Json format to php page then after decoding it send to mysql database,but if my string contains < > || & ' " characters then my web page gives php error.so how should i proceed further. It doesn't allow for inserting some special characters ..

 var obj = {"comment": commentText, "postID": postID};
                var commentData = 'commentData=' + JSON.stringify(obj);


                $.ajax({
                    type: "POST",
                    url: addNewCommentsUrl,
                    datatype: 'json',
                    data: commentData,
                    cache: false,
                    beforeSend: function() {
//                 $(document.body).off('click','.postComment');

                    },
                    success: function(result) {

                        commentBox.val("");
                        commentHolder.append(result);
//                     jQuery("#all-posts").masonry('reloadItems');
                        jQuery('#all-posts').masonry('layout');

                        var count = parseInt(parent.find("#commentContainer").text());
                        parent.find("#commentContainer").html(++count);

//                    $(document.body).on('click','.postComment');
                    }

                });// end of ajax

at php side

 $recievedData = $_POST['commentData'];
            $recieveddatajson = json_decode($recievedData);
            $lastCommentID = $recieveddatajson->{'commentID'};
            $parentPostID = $recieveddatajson->{'postID'};
  • 写回答

1条回答 默认 最新

  • dtcpvz8162 2014-05-07 12:28
    关注

    Okay, I see what you are doing. You don't need to do that. Here's a cut down version of your problem, showing you how to achieve ajax post:

    test.php

    <?php
    
    // Handle Post
    if (count($_POST))
    {
        echo "You posted
    ";
        print_r($_POST);
        exit();
    }
    
    ?>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function()
    {
        $.ajax({
            type: "POST",
            url: "test.php",
            data: { "comment": "hello world", "postID": 1234 },
            // data: { "comment": commentText, "postID": postID },
            success: function(result) {
                alert("Server Said:
    " + result);
            }
        });
    });
    
    </script>
    

    Outputs:

    enter image description here


    So when the request occurs, data fields and their values are available in php like this:

    $comment = isset($_POST['comment']) ? $_POST['comment'] : '';
    $postID = isset($_POST['postID']) ? $_POST['postID'] : '';
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?