0

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'};
5
  • 2
    Start for reading the error Commented May 7, 2014 at 10:33
  • what is the error? and can you show your code? Commented May 7, 2014 at 10:40
  • 1
    @LatheesanKanes It gives normal error i.e cannot insert null value to column. Commented May 7, 2014 at 10:43
  • @LatheesanKanes actually problem arise when encode and decode through json format when string contains above special characters Commented May 7, 2014 at 10:44
  • Can you show us a sample data source being posted via ajax and the code for making the ajax request and the php post handler php code. Commented May 7, 2014 at 10:52

1 Answer 1

2

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\r\n";
    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:\r\n" + 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.