首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jquery.html()将html元素保存到数据库中

使用jquery.html()将html元素保存到数据库中
EN

Stack Overflow用户
提问于 2014-03-20 06:18:03
回答 1查看 1.2K关注 0票数 1

我有一个问题,我搜索了很多,但找不到任何解决方案,所以决定发布一个问题。我有一个网页,它是生成的,需要我将整个附加的网页保存到数据库中。我使用php和mysql.For保存我正在使用的数据:

代码语言:javascript
复制
$(function(){htmldata=$('body').html();});

有了这个,我就可以存储整个表单和脚本。但问题是,当我开始从数据库调用值时,html数据显示得很好,但是脚本都搞砸了。

从数据库调用时,我使用的是

代码语言:javascript
复制
$html = trim(addslashes(htmlspecialchars(
                        html_entity_decode($_POST['htmldata'], ENT_QUOTES, 'UTF-8'),
                        ENT_QUOTES, 'UTF-8'
                    )));

对于这个问题,有什么可能的解决方案呢?我想这只适用于没有script.Please帮助的数据。

这就是我的脚本在从数据库调用后的样子。

代码语言:javascript
复制
function test()
    {
        var formData = form2object('testForm', '.', true,

                function(node)
                {
                    if (node.id && node.id.match(/callbackTest/))
                    {
                        return { name: node.id, value: node.innerHTML };
                    }
                });

就像“被添加”

我的原始脚本

代码语言:javascript
复制
function test()
    {
        var formData = form2object('testForm', '.', true,

                function(node)
                {
                    if (node.id && node.id.match(/callbackTest/))
                    {
                        return { name: node.id, value: node.innerHTML };
                    }
                });
EN

回答 1

Stack Overflow用户

发布于 2014-03-20 06:35:20

您可以使用以下方法,

代码语言:javascript
复制
//Function to remove slashes
    function striptext($Str)
        {   
            //Condition for not empty
            if($Str != "") 
            {
                //Condition to check whether the string is not numeric
                if(!is_numeric($Str))
                   return stripslashes(trim($Str));
                else
                    return trim($Str);
            }
            else
                return $Str;
        }

        //Function to add slashes
        function wraptext($Str)
        {
            //Condition for not empty
            if(!empty($Str))
            {
                //Condition to check whether the string is not numeric
                if(!is_numeric($Str))
                {
                    //To check whether the function exists or not
                    if(function_exists('mysql_real_escape_string'))
                        return mysql_real_escape_string(htmlspecialchars(stripslashes(trim($Str)), ENT_QUOTES));
                    else
                        return addslashes(htmlspecialchars(stripslashes(trim($Str)), ENT_QUOTES));
                }
                else
                    return trim($Str);
            }
            else
                return $Str;
        }

So while getting post values you can use,

$html = wraptext($_POST['htmldata']);

And while fetching and displaying from database you can use,

echo striptext($your_variable);

And for displaying datas with script tags you can use,

echo striptext(html_entity_decode($your_variable, ENT_QUOTES));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22524916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档