【问题标题】:Save text of textarea in db将文本区域的文本保存在数据库中
【发布时间】:2014-01-09 01:41:24
【问题描述】:

问题如下

  • 我必须保存从 html 页面获取的 textarea 的内容
  • 通过 $.ajax 调用将其发送到 php 文件
  • 将 textarea 的内容写入数据库
  • 用另一个 php 文件从数据库中读取 textarea 的内容而不会丢失文本的格式
  • 在另一个文本区域中显示恢复的文本

例子

保存

你好 你好 你好

显示

你好 你好 你好

而不是你好你好

我试过这样,但我失去了文本的格式

我的html页面

<textarea id="materiale1" style="margin-top: 0px;width: 715px;height:70px;max-width:715px;max-height:70px;" class="k-textbox"></textarea>

<a href="#" onclick="scrivi();return false;"><span class='k-button' style="margin-top: 0px;"><h3>SCRIVI</h3></span></a>

<textarea id="materiale2" style="margin-top: 80px;width: 715px;height:70px;max-width:715px;max-height:70px;" class="k-textbox"></textarea>

<a href="#" onclick="leggi();return false;"><span class='k-button' style="margin-top: 72px;"><h3>LEGGI</h3></span></a>

调用函数将 textarea 文本发送到文件 php

function scrivi(){

$.ajax({ 
    type: "GET",
    url : "test_scrivi.php?testo="+$('#materiale1').val(),
    dataType:'json',
    cache: false,
    success: function(dati){ 

     }
     , error : function(errore){ 
     } 
    });
}

文件 php (test_scrivi.php) 用于保存 textarea 的文本

<?php


 try {

include('../../login/connect_db.php');

$db = new PDO("mysql:host=$my_hostname;dbname=$my_db_name", $my_username, $my_password);

$testo = nl2br($_GET['testo']);

$query = "UPDATE test SET testo='".$testo."' WHERE id = 1";

$result = $db->query($query);


} 
catch(PDOException $e)
{
        //echo $e->getMessage();
}


?>

调用读取文本区域文本的函数

function leggi(){

    $.ajax({ 
            type: "GET",
            url : "test_leggi.php",
            dataType:'json',
            cache: false,
            success: function(dati_arrivati){ 

                $('#materiale2').val(dati_arrivati)

            }
            , error : function(errore){ 
              } 
        });


}

文件 php (test_leggi.php) 用于读取 textarea 的文本

<?php


try {

    include('../../login/connect_db.php');

    $db = new PDO("mysql:host=$my_hostname;dbname=$my_db_name", $my_username, $my_password);

    $query = "SELECT * FROM test WHERE id = 1";

    $result = $db->query($query);

    header("Content-type: application/json");

    $testo = "";

    foreach($result as $row)
        {

            $testo = nl2br($row['testo']);


        } 

    echo json_encode($testo);

    $db = null;

} 
catch(PDOException $e)
{
        //echo $e->getMessage();
}


?>

【问题讨论】:

  • 每次循环都会覆盖变量$testo,所以test_leggi.php中只会得到一行而不是全部。
  • 是的,这只是一个测试,表中只有一行 id = 1

标签: php jquery html ajax textarea


【解决方案1】:

将您的 AJAX 调用更改为:

$.ajax({ 
    type: "GET",
    url : "test_scrivi.php",
    data: { testo: $('#materiale1').val() },
    dataType:'json',
    cache: false,
    success: function(dati){ 

     },
    error : function(errore){ 
     } 
});

您没有正确地对值进行 URL 编码。当您使用数据对象时,jQuery 会正确执行此操作。

【讨论】:

  • 完美,问题出在ajax调用此时我删除了函数nl2br 谢谢!
  • 您仍然需要nl2br,因为当您将值放回HTML 时,它需要标记。
  • 我认为问题出在视图上。你打电话的地方: echo json_encode($testo);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
相关资源
最近更新 更多