【问题标题】:Sending Html Text via ajax (html) to php script to store in db. It only saves until  通过 ajax (html) 将 Html 文本发送到 php 脚本以存储在数据库中。它只保存到
【发布时间】:2015-09-28 01:32:26
【问题描述】:

我得到以下js脚本提交表单html内容:

    $.ajax({
        url : 'include/speakers.php',
        type : 'POST',
        data : "htmlText="+htmlField",
        dataType : 'html',
        success : function (result) {
        },
        error:function (xhr, ajaxOptions, thrownError){
            $( ".phpMessage" ).html( " - "+thrownError );
        }
    });

演讲者 Php 看起来像:

$htmlTextVar = $_POST['htmlText'];
$stmt = $mysqli->prepare("UPDATE table SET htmlText = ? WHERE id = ?");
$stmt->bind_param('si', $htmlTextVar, $id);  
$stmt->execute(); 

现在,问题是当我提交像

这样的 html 文本时
<p class="MsoNormal"><span lang="EN-US">&nbsp;</span>

它只存储文本直到&amp;nbsp;。所以数据库表中的文本是

&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;

不知道如何解决这个问题:(

【问题讨论】:

  • 显示htmlField声明

标签: php html mysql ajax encoding


【解决方案1】:

把你的数据放在encodeURIComponent()

$.ajax({
    url : 'include/speakers.php',
    type : 'POST',
    data : "htmlText=" + encodeURIComponent(htmlField),
    dataType : 'html',
    success : function (result) {
    },
    error:function (xhr, ajaxOptions, thrownError){
        $( ".phpMessage" ).html( " - "+thrownError );
    }
});

在你的 php 文件中

$htmlTextVar = urldecode($_POST['htmlText']);

【讨论】:

  • @jQuery 如果它解决了你的问题,你可以点击它作为接受的答案:)
猜你喜欢
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2015-10-15
  • 1970-01-01
  • 2023-03-26
  • 2022-11-13
  • 2013-05-07
相关资源
最近更新 更多