javascript - Ajax 没有将数据完全发布到 php 文件

标签 javascript php ajax

Ajax 没有将整个字符串数据发送到 php 脚本时出现了一个非常奇怪的错误。

字符串是

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;
font-family:&quot;Verdana&quot;,sans-serif;mso-ansi-language:DE">Gold GoldGold Gold Gold Gold<o:p></o:p></span></u></b></p>

<p class="MsoNormal" style="text-align:justify"><span lang="EN-GB" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;font-family:&quot;Verdana&quot;,sans-serif">&nbsp;</span></p>"

但是到达 php 脚本的只是(通过 DB 提交和 php $result 测试看到的。

"<p class="MsoNormal" style="text-align:justify"><b><u><span lang="DE" style="font-size:10.0pt;mso-bidi-font-size:11.0pt;
font-family:"

我尝试通过多种方法检查为什么会发生这种情况,但根本无法弄清楚。

这是我的 Javascript 代码和 PHP 代码。

JS:

function Send_upload_news()
    {
        get_title = document.getElementById('Germ_title');
        get_date = document.getElementById('Germ_date');

        input_title = get_title.value;
        input_date = get_date.value;
        input_content = $("div.nicEdit-main").html();

        //console.log(input_content);

        var Data = '&input_title='+input_title+'&input_date='+input_date+'&input_content='+input_content;
        //console.log(Data);
        $.ajax({
            url : "uploadNews.php",
            type: "POST",
            dataType: 'text',
            data : Data,
            success: function(result){alert(result);},
            /* success: function() {
                 alert('Post has been uploaded to Database');
            }, */
           error: function(XMLHttpRequest, textStatus, errorThrown) {
           alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
           }
        });
        nicEditors.findEditor( "Germ_content" ).setContent( '' );
        get_title.value = "";
        get_date.value = "";
        $('html, body').animate({ scrollTop: 0 }, 'slow');

    };

pHp:(uploadNews.php)

<?php
//Database info
$db_host = "localhost";
$db_username = "";
$db_pass = "";
$db_name = "";
//connect db    
$connMS = new mysqli ( $db_host, $db_username, $db_pass, $db_name );
//grab data 

$this_title = $_POST['input_title'];
$this_date = $_POST['input_date'];
$this_content = $_POST['input_content'];

$result = file_put_contents ( "test.txt", $this_content);
//create query and execute  
$sqlupdate_news = "INSERT into news_content(germ_title, germ_date, germ_content) values ('$this_title','$this_date','$this_content')";
mysqli_query ($connMS,$sqlupdate_news); 
//close
mysqli_close($connMS);
?>

我使用 WYSWYG Nicedit 作为我的文本区域

如果有人可以帮助我解决这个问题,我将不胜感激。

最佳答案

您的代码存在的问题是您没有对值进行编码。所以这会弄乱值(value)观。另外,您有一个前导 & ,这是没有意义的。您可以使用 encodeURIComponent 手动编码所有值,也可以让 jQuery 为您处理。

当您使用对象时,jQuery 会为您进行编码。

function Send_upload_news() {

    var get_title = document.getElementById('Germ_title');
    var get_date = document.getElementById('Germ_date');

    var Data = {
        input_title : get_title.value,
        input_date : get_date.value,
        input_content : $("div.nicEdit-main").html()
    };

    $.ajax({
        url : "uploadNews.php",
        type: "POST",
        dataType: 'text',
        data : Data,
        success: function(result){alert(result);},
       error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert('There is an error, screenshot this error and send to Admin : ' +textStatus+" - "+errorThrown);
       }
    });

    nicEditors.findEditor( "Germ_content" ).setContent( '' );
    get_title.value = "";
    get_date.value = "";
    $('html, body').animate({ scrollTop: 0 }, 'slow');
}

你真的应该使用 var 这样你的应用程序就不会充满全局变量。

关于javascript - Ajax 没有将数据完全发布到 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376172/

相关文章:

javascript/jquery 输入验证

javascript - Babel 没有将导入的 node_modules 转译为 ES5 - 包括 ES2015 语法

PHP DOM获取nodevalue html? (不剥离标签)

php - 分页链接在 codeigniter 中无法正常工作

javascript - 触发两个脚本 : one that loads data and another that manipulates styling? 的正确方法是什么

javascript - 在这种情况下我如何使用 javascript 创建一个 Promise 函数

javascript - 我可以从 places.sqlite 检索网站图标吗?

php - 引用:什么是变量范围,哪些变量可从何处访问,什么是“ undefined variable ”错误?

php - 从时间戳 SQL 获取日期

javascript - 如何检测多个 AJAX 请求何时完成或失败?