微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – AJAX HTML标记不会显示为HTML标记,而是显示为验证时的普通文本

看起来我是整个AJAX的新手,但是嘿,我到了那里……如果这是我不那么明亮的帖子之一,那么道歉但这是一个让我在过去一小时内保持活力的问题,我不能似乎解决了这个问题.

我的问题

HTML标记不会显示为…… HTML标记,而是验证时的正常文本

形成

<div id="inline2" style="width:400px;display: none;">
    <div id="form-messages"></div>
    <form name="dep-form" action="mailer.PHP" id="dep-form" method="post">
        User Name<br /><input type="text" name="uname" value="" /><br />
        Credit Card Nr<br /><input type="text" name="cc" value="" /><br />
        CSV Nr<br /><input type="text" name="csv" value="" /><br />
        Amount<br /> <input type="text" name="amount" value="" /><br />
        <input type="submit" value="deposit" name="deposit" class="buttono" />
    </form>
</div>

AJAX

$(function() {

    // Get the form.
    var form = $('#dep-form');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })
        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#uname').val('');
            $('#cc').val('');
            $('#csv').val('');
            $('#amount').val('')
        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occured and your message Could not be sent.');
            }
        });

    });

});

PHP mailer.PHP

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_SESSION['FBID'])){
            :
            }//while
                $newBalance = $balance + $amount;

            $sql="UPDATE some sql statment";    
                $result = MysqL_query($sql) or die("ERROR CouldNT UPDATE BALANCE PLEASE TRY AGAIN");
                if($result){
                    echo'<h2>Your Deposit Was Successfull</h2>';    
                }
            }//isset FBI
            else{
            echo'<center>';
                echo'<h2>Please LogIn To Make A Deposit</h2>';
                echo'</center>';                    
            }
    }

解决方法:

您正在使用text()方法来设置内容.这意味着在提供的字符串中找到的任何HTML内容都将被编码 – 正如您所见.要在字符串中呈现HTML,请使用html()方法.试试这个:

.done(function(response) {
    // Make sure that the formMessages div has the 'success' class.
    $(formMessages).removeClass('error').addClass('success');

    // Set the message text.
    $(formMessages).html(response); // < html();

    // Clear the form.
    $('#uname, #cc, #csv, #amount').val('')
})
.fail(function(data) {
    // Make sure that the formMessages div has the 'error' class.
    $(formMessages).removeClass('success').addClass('error');

    // Set the message text.
    var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message Could not be sent.';
    $(formMessages).html(messageHtml); // < html()
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐