【问题标题】:jQuery AJAX JSON - textarea characters issuejQuery AJAX JSON - 文本区域字符问题
【发布时间】:2011-10-15 22:17:22
【问题描述】:

我有一个评论系统的代码。

jQuery:

function sendComment() {
$('#comment_write_area').fadeOut(1);
$('#ajax-json-response').fadeOut('fast').html('');
$('#ajax-loading').html('<img src="images/ajax-loader.gif" />').fadeIn('slow');
var comment = $('#comment').val();
var news_id = $('#news_id').val();
var user_id = $('#user_id').val();
var logged_in = $('#logged_in').val();
var number = $('#number').val();
$.ajax({
    type: 'POST',
    url: 'json.php',
    dataType: 'json',
    cache: false,
    timeout: 20000,
    data: { a: 'comment_add', comment: comment, news_id: news_id, user_id: user_id, logged_in: logged_in, number: number },
    success: function(data) {
        $('#ajax-loading').fadeOut(1).html('');
        $('#comment_write_area').slideToggle('slow');
        $('#comment').val('');
        $('#ajax-json-response').html(data.message).fadeIn('slow');
        if (!data.error) {
            $('input#number').val(data.nextnumber);
            $('#ajax-response').after(data.comment);
            $('#comment_'+data.nextnumber).slideToggle('slow');
            $('#no-comment').fadeOut('slow');
        }
    },
    error: function(jqXHR, textStatus, errorThrown) {
        $('#ajax-loading').fadeOut(1).html('');
        $('#comment_write_area').fadeIn('slow');
        $('#comment').val('');
        //$('#ajax-json-response').html('Probléma történt! Kérlek próbáld újra később! (HTTP Error: '+errorThrown+' | Error Message: '+textStatus+')').fadeIn('slow');
        $('#ajax-json-response').html('Probléma történt! Kérlek próbáld újra később, és ellenőrizd az internetkapcsolatod!').fadeIn('slow');
    }
}); }

json PHP 是这样的:

elseif ($_POST['a'] == 'comment_add') {
if ($_POST['logged_in']) {
    if (!empty($_POST['comment']) && !empty($_POST['news_id']) && !empty($_POST['user_id'])) {
        $number = $_POST['number'] + 1;
        $_POST['comment'] = $do->htmlEncode($_POST['comment']);
        $return['nextnumber'] = $number;
        $return['error'] = false;
        $return['message'] = '<font style="color: green;">'.$nsite->json('DO_COMMENTS_SUC').'</font>';
        $do->mysqlSelect('user', 'icl_users', 'WHERE id = "'.$_POST['user_id'].'"');
        mysql_query("INSERT INTO icl_comments (name, content, news_id, date) VALUES ('".$do->msS('user', 'username')."', '".nl2br($_POST['comment'])."', '".$_POST['news_id']."', '".date("Y-m-d H:i:s")."')");
        $do->mysqlSelect('comment', 'icl_comments', 'WHERE `name` = "'.$do->msS('user', 'username').'" ORDER BY id DESC LIMIT 1');
        $return['comment'] = $do->genComment($do->msS('comment', 'id'), $do->msS('user', 'username'), $do->msS('user', 'id'), $do->msS('user', 'userpic'), $number, date("Y-m-d H:i:s"), $_POST['comment'], $site_color);
    } else {
        $return['error'] = true;
        $return['message'] = '<font style="color: red;">'.$nsite->json('DO_COMMENTS_DEL_ERROR_3').'</font>';
    }
} else {
    $return['error'] = true;
    $return['message'] = '<font style="color: red;">'.$nsite->json('POLLS_NO_LOGIN').'</font>';
}
echo json_encode($return); }

我的问题是,当我将'" 字符写入文本区域时,它会显示',如下所示:\'。在我刷新网站并从 MySQL 读取数据后,它显示完美。

你能告诉我原因吗?

谢谢!

【问题讨论】:

    标签: jquery html ajax json textarea


    【解决方案1】:

    php 函数 stripslashes() 可能会有所帮助...

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多