php - 保存回数据库时用\ n替换\ n

时间:2017-06-19 15:37:41

标签: javascript php jquery html ajax

我只是想先说,我已尝试在其他地方进行搜索,并且有很多答案可以用\n代码替换<br>

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

我有一个文本框,其中填充了数据库中的文本。 enter image description here

这是使用 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,编辑注释并单击竖起大拇指并将其直接保存回数据库

所以,如果我在这里看到<br>enter image description here

然后提交该更改,并确实替换为:enter image description here

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

如果我在那里添加了stackoverflow这个词,它现在会重新加载带有单词的音符,但我们不再有换行符: enter image description here

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

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;
};

submitSQLNoteText.php文件:

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));
    }
}

那我为什么要丢掉换行符?

0 个答案:

没有答案