TinyMCE + Jquery + PHP + AJAX特殊字符问题 [英] TinyMCE + Jquery + PHP + AJAX Special chars issue

查看:153
本文介绍了TinyMCE + Jquery + PHP + AJAX特殊字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个 TinyMCE 实现,效果很好.但是,如果用户输入特殊字符,如<"当出现特殊字符时,发送到我的 MySQL 数据库的查询就会被裁剪.让我告诉你什么令人开心:

i have a TinyMCE implementation on my website that works great. But if the user enters a special char like "<" the query send to my MySQL database gets cropped just when the special char appears. Let me show you whats happining:

这就是我所说的TinyMCE:

This is how i call TinyMCE:

tinyMCE.init({
        mode : "exact", elements : "e_text", theme : "simple", oninit : "setPlainText", plugins : "paste" });
        function setPlainText() {
            var ed = tinyMCE.get('e_text');
            ed.pasteAsPlainText = true;  
            if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {
                ed.onKeyDown.add(function (ed, e) {
                if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))
                    ed.pasteAsPlainText = true;
                });
            } else {            
                ed.onPaste.addToTop(function (ed, e) {
                ed.pasteAsPlainText = true;
            });
            }
        }

不用担心在那里查找错误,该代码使TinyMCE呈现无错误.然后我有一个保存ajax进程的jquery函数:

Dont worry about searching for an error there, that code renders the TinyMCE with no errors. Then i have a jquery function that holds the ajax process:

    $("#save-btn").click(function() {
            tinyMCE.triggerSave();
            alert($('#e_text').val());
            $("#status").html('<img src="css/img/ajax-loader.gif" align="absmiddle">&nbsp;Saving...');
            $.ajax({type: "POST",   url: "handler.php", data: "e_text="+ $('#e_text').val() + "&e_title="+ $('#extra_title').val(),
                        success: function(server_response){
                                $("#status").ajaxComplete(function(event, request){
                                alert(server_response);
                                        if(server_response.substring(0,4) == 'Last') {
                                            $("#status").html('<img src="css/img/available.png" align="absmiddle"> <font color="#06699e"> '+ server_response +' </font>&nbsp; ');
                                        }
                                        else {
                                            $("#status").html('<img src="css/img/not_available.png" align="absmiddle"> <font color="red">Not saved, please try again later. '+server_response+'</font>');
                                        }

                                });
                        }

                });
    });

此调用handler.php将处理ajax调用,然后返回结果.对于这个特殊的问题,我修改了php脚本,只是为了打印POST var并查看发生了什么,或者换句话说,是什么到达了php脚本:

This call handler.php wich handle the ajax call and then return a result. For this particular problem i modify the php script just to print the POST var and see what is going on, or in other hands, what is arriving to the php script:

} elseif(isset($_POST['e_text'])) { 
    $fgmembersite->DBLogin();
    echo($_POST['e_text']);
    mysql_query("INSERT INTO ".table_extra." (id,id_user,title,content) VALUES (NULL,'".$_SESSION['id_of_user']."','".mysql_real_escape_string($_POST['e_title'])."','".mysql_real_escape_string($_POST['e_text'])."') ON DUPLICATE KEY UPDATE title='".mysql_real_escape_string($_POST['e_title'])."', content='".mysql_real_escape_string($_POST['e_text'])."';");
    mysql_query("UPDATE ".$fgmembersite->tablename." SET last_update_cv = NOW();");
    $result = mysql_fetch_array(mysql_query("SELECT NOW();"));

    //echo 'Last saved ' . $result['NOW()'];
}

忽略 echo 指令之外的所有内容.返回我的jquery代码,您还记得两个警报功能吗?好吧,其中一个向我展示了TinyMCE返回的数据,然后再将其发送到php脚本,第二个向我展示了由php脚本返回的数据.

Ignore everything but the echo instruction. Returning to my jquery code you remember the two alert functions? Well one of them show me the data returned by the TinyMCE before sending it to the php script and the second one show the data returned data by the php script.

让我们假设我在TinyMCE中插入短语" Hello world >>>再次问好!",这是我看到的:

Lets suppose i insert the phrase "Hello world >>> Hello again!" into the TinyMCE, this is what i see:

然后我点击保存",并得到了第一号警报:

Then i hit Save, and i got the alert number one:

我点击确定,并得到第二个警报(服务器响应,php得到了什么):

I hit ok and i get the alert number two (the server response, what php got):

所以,为什么?!!为什么php脚本(只是和通过POST得到的回声)返回了一个错误的短语?只是何时出现特殊字符?

SO, WHY?!!! why is the php script (just and echo of what it gets via POST) is returning a cropped phrase? Just when the special char appear?

感谢您能为我提供的任何帮助!

Thanks for any help you can offer me!

推荐答案

那些&符使data属性的URL格式混乱. 为什么不将其更改为类似这样的内容?

Those ampersands are messing with the URL format for the data property. Why not change it to something like this instead?

data: {
    "e_text": $('#e_text').val(),
    "e_title": $('#extra_title').val()
}

这篇关于TinyMCE + Jquery + PHP + AJAX特殊字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆