Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript php-将\n替换为<;br>;保存回数据库时_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript php-将\n替换为<;br>;保存回数据库时

Javascript php-将\n替换为<;br>;保存回数据库时,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我只是想说,首先,我已经尝试在其他地方搜索过了,对于用标记替换\n有很多答案 然而,我有一点奇怪的问题 我有一个文本框,其中填充了来自数据库的文本。 这是使用以下php加载的: if (isset($_POST['sqlnoteid'])) { if (empty($_POST['sqlnoteid'])) { $notes = 'No Data'; } if (sqlsrv_has_rows(

我只是想说,首先,我已经尝试在其他地方搜索过了,对于用

标记替换
\n
有很多答案

然而,我有一点奇怪的问题

我有一个文本框,其中填充了来自数据库的文本。

这是使用以下php加载的:

if (isset($_POST['sqlnoteid'])) 
    {
        if (empty($_POST['sqlnoteid'])) 
        {
            $notes = 'No Data';
        }
        if (sqlsrv_has_rows($stmt)) 
        {
            $data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);

            echo "<div class='custom-font title-container'>
                    <div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
                    <div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editSQLNote()'>&nbsp;&nbsp;&nbsp;</div>" . "<div data-toggle='modal' data-target='#editSQLNoteNameModal' class='display-inline'>" . $data['SQLNoteName'] . "</div>" . "&nbsp;<div class='save-note fa fa-thumbs-up' onclick='saveSQLNote(); submitSQLNoteText();'></div></div>
                  </div>";

            $note = nl2br($data['SQLNote']);
            $note = preg_replace('%(SELECT|FROM|WHERE|INNER|JOIN|DISTINCT|LEFT|OUTER|APPLY|WITH|NOLOCK|DECLARE|ORDER BY|VARCHAR|bit)%m', '<span style="color: red;">$1</span>', $note);
            $note = preg_replace('%(ISNULL|UPDATE|SET|INSERT INTO)%m', '<span style="color: #9A2EFE;">$1</span>', $note);          
            $note = preg_replace('%(IS NOT NULL|AND|IS NULL)%m', '<span style="color: grey;">$1</span>', $note);
            $note = preg_replace('%(BEGIN TRAN|ROLLBACK TRAN|BEGIN|END|CASE|END|WHEN|THEN|ELSE)%m', '<span style="color: blue;">$1</span>', $note);
            $note = preg_replace('%(GETDATE|MAX|CONVERT|UPDATE|CAST|COUNT)%m', '<span style="color: #FA58F4;">$1</span>', $note);
            $note = preg_replace('%(@.+?\b)%m', '<span style="color: #009EC0; font-style: italic;">$1</span>', $note);
            $note = preg_replace('%(\[(.+?)\])%m', '<span style="font-weight: bold;">$1</span>', $note);
            $note = preg_replace('%(--.+?\n)%m', '<span style="color: green;">$1</span>', $note);
            $note = preg_replace('%(codestart)%m', '<code>', $note);
            $note = preg_replace('%(codeend)%m', '</code>', $note);

            echo "<div contenteditable='false' id='ta4'>" . $note . "</div>";
        } 
        else 
        {
            echo "No data found";
        }
    }
然后,我可以单击铅笔图标,使用ajax编辑便笺,单击竖起的大拇指,并将其直接保存回数据库

因此,如果我输入一个

,如下所示:

然后提交更改,它确实会这样替换:

我现在可以刷新页面了,等等。直到我进行另一次编辑,它必须再次从数据库加载注释

如果我在其中添加单词stackoverflow,它现在将用该单词重新加载便笺,但我们不再有换行符:

这是我在保存更改时提交回数据库的代码:

onclick="submitSQLNoteText();"
它运行:

function submitSQLNoteText()
{
    var noteid = <?php if(isset($_POST['sqlnoteid'])){ echo $_POST['sqlnoteid'];} ?>;
    var notetext = $("#ta4").text();
    var data = {noteid1: noteid, notetext1: notetext};

    if(noteid == ''||notetext == '')
    {
        alert("NoteID or Text is blank");
    }
    else
    {   
        $.ajax({
            type: "POST",
            url: "submitSQLNoteText.php",
            dataType: 'json',
            data: data,
            cache: false,
            success: function(result){
                alert("Success");
            }
        });
    }
    return false;
};

那么为什么我会丢失换行符呢?

您似乎还有一些html标记需要勾选
`
,以便突出显示它们。像

这样没有显示的,可能更多。编辑:我为你修正了:LukeLitherlandMight对你来说有点低TEC,但是你考虑过<代码> StReSpple(“\n”,'Br>,$ASILIN);
@RiggsFolly我确实有,但运气不好:(@Fred ii-谢谢!我下次会记住的。@LukeLitherland欢迎。你可能需要为此添加(更多/更多)HTML。我可能帮不上忙,但其他人可能会发现一些东西。这也可能与JSON和
HTML\u entity\u decode()有关。)
但我不能100%肯定。
if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{

    $noteid2 = $_POST['noteid1'];
    $notetext2 = html_entity_decode($_POST['notetext1']);

    $stmt = "UPDATE SQLNotes SET SQLNote = (?) WHERE SQLNoteID = (?)";
    $params = array($notetext2, $noteid2);

    $stmt = sqlsrv_query($conn, $stmt, $params);

    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }
}