Php 如何将数据从浏览器发送到服务器并回复

Php 如何将数据从浏览器发送到服务器并回复,php,javascript,character-encoding,url-encoding,Php,Javascript,Character Encoding,Url Encoding,在我的网页上有一个文本区,用户在其中输入各种字符。 当他保存数据时,我想使用AJAX将数据发送到服务器,以便存储在数据库中。之后,服务器会将数据发送回浏览器以显示 哪种方法是正确的? 现在我做一个AJAX调用: function update() { $.ajax({ type: "POST", url: "ajax/update.php", data: "ID=" + ID + "&value=" + encodeURICompo

在我的网页上有一个文本区,用户在其中输入各种字符。
当他保存数据时,我想使用AJAX将数据发送到服务器,以便存储在数据库中。之后,服务器会将数据发送回浏览器以显示

哪种方法是正确的?
现在我做一个AJAX调用:

function update()
{
    $.ajax({
        type: "POST",
        url: "ajax/update.php",
        data: "ID=" + ID + "&value=" + encodeURIComponent($('#newText').val()),
        success: function(html) {
            $('#l' + CurrentID).html(decodeURIComponent(html));
        }
    });
}
我的php文件如下所示:

<?php

$ID = $_POST["ID"];
$value = nl2br(urldecode($_POST["value"]));
if (get_magic_quotes_gpc()) {
    $value = stripslashes($value);
}
$value = htmlentities($value);

$value = str_replace("<br />", "", $value);
$value = str_replace("<br/>", "", $value);
$value = str_replace("<br>", "", $value);

mysql_query("update MyTable set value = '$value' where id = $ID");

echo html_entity_decode(urlencode($value));
?>
$.ajax({
    type: "POST",
    url: "ajax/update.php",
    data: { ID: ID, value: $('#newText').val() },
    success: function(html) {
        $('#l' + CurrentID).html(html);
    }
});

您可以这样改进AJAX调用:

<?php

$ID = $_POST["ID"];
$value = nl2br(urldecode($_POST["value"]));
if (get_magic_quotes_gpc()) {
    $value = stripslashes($value);
}
$value = htmlentities($value);

$value = str_replace("<br />", "", $value);
$value = str_replace("<br/>", "", $value);
$value = str_replace("<br>", "", $value);

mysql_query("update MyTable set value = '$value' where id = $ID");

echo html_entity_decode(urlencode($value));
?>
$.ajax({
    type: "POST",
    url: "ajax/update.php",
    data: { ID: ID, value: $('#newText').val() },
    success: function(html) {
        $('#l' + CurrentID).html(html);
    }
});
注意数据是如何使用对象发送的。您不再需要担心正确的url编码。jQuery负责处理它


此外,您的PHP似乎容易受到SQL注入的攻击。您应该使用,以避免出现这种情况。

对于php部分?现在这样行吗?我不知道为什么人们要自己建立查询字符串。。您会看到很多这样的问题。@Cristian Calu,您的PHP部分似乎容易受到SQL注入的攻击。我不是PHP方面的专家,但看看SQL查询中的$value和$ID,恐怕情况就是这样。