开发者

Posting HTML content to PHP via Ajax

I'm posting HTML content via AJAX to PHP. During debugging process i see that ajax posts whole content from editor. But when i'm checking my db I see 40-50% of sent content. Filtering sent data with following function in php. My database field type is text with 0 length.

function html($data, $db)
{
$data = htmlentities($data);        
$data = $db->escape_string($data);
return $data;
}

No success. But when i'm trying to post standard text content (none-html) it posts whole content into db table. How to deal with that problem? Any suggestions?

JS

function postViaAjax(autosaveMode) {
    var name = $("#name").val();
    var title = $("#title").val();
    var menu = $("#menu").val();
    var parentcheck = $(".parentcheck:checked").val();
    var id = $("#id").val();
    if (parentcheck == 0) {
        var parent = parentcheck;
    } else {
        var parent = $("#parent").val();
    }
    var content = CKEDITOR.instances['content'].getData();
    var dataString = 'name=' + name + '&title=' + title + '&menu=' + menu + '&parentch开发者_如何学Ceck=' + parentcheck + '&id=' + id + '&parent=' + parent + '&content=' + content;
    $.ajax({
        type: "POST",
        url: "processor/dbadd.php",
        data: dataString,
        dataType: "json",
        success: function (result, status, xResponse) {
            var message = result.msg;
            var err = result.err;
            var now = new Date();
            if (message != null) {
                if (autosaveMode) {
                    $('#submit_btn').attr({
                        'value': 'Yadda saxlanıldı ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds()
                    });
                } else {
                    $.notifyBar({
                        cls: "success",
                        html: message + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds()
                    });
                }
            }
            if (err != null) {
                $.notifyBar({
                    cls: "error",
                    html: err
                });
            }
        }
    });
};


Most likely because you need to escape your "content" when you're posting it.

Try

content = encodeURIComponent(content) 

or

content = encodeURI(content)

Before appending it to the dataString


I think you may be thinking about this the wrong way. The PHP script that you have the callback function on will receive all the data which you tell it to receive.

If you are using a library such as jQuery, you can set something up so its a bit like the following:

    var formValues = $(this).serialize();
    $.ajax({
        url: 'yourFile.php?'+formValues,
        dataType: 'json',
        success: function(data){
            console.log(data);
        }
    }); 
    return false;

Then you will just have to have your php script pick up the data which is being sent. You can do that via POST or GET it is entirely up to you and depending on what you are doing.

I hope this helps get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜