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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示