javascript - AJAX HTML 标签不会显示为 HTML 标签,而是在验证时显示为普通文本

标签 javascript php jquery html ajax

看,我是整个 AJAX 方面的新手,但嘿,我已经到达那里了...因此,如果这是我不太聪明的帖子之一,我深表歉意,但这是一个让我在过去一个小时内保持清醒的问题,我似乎无法修复它。

我的问题

HTML 标签不会显示为....以及 HTML 标签,而是正常的验证文本

enter image description here

表格

<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 邮件程序.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()
});

关于javascript - AJAX HTML 标签不会显示为 HTML 标签,而是在验证时显示为普通文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427331/

相关文章:

javascript - Web 组件 : how to access a slotted element using shadowRoot. querySelector

javascript - 使用非标准的 HTML 属性是否被认为是不好的做法?

php - PHP跨脚本读写全局变量

javascript - jQuery .post() 未执行

javascript - 使用 adobeomnture 实现动态页面标题

javascript - 全日历最大时间进入第二天

javascript setInterval 看不到 .css jquery

javascript - React Native .then 似乎运行不正常

php - 将更改的变量分配给同一变量是否安全?

php - 如何将 SQL Server 与 Yii2 连接