Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX HTML tags does NOT get displayed as HTML tags but as normal text on validation

Look I'm a rookie with the whole AJAX thing but hey I'm getting there...Thus apologies if this is one of my less brighter posts but this is a problem that is keeping me up for the past hour, and I cant seem to fix it.

My problem

HTML tags does NOT get displayed as....well HTML tags, but rather normal text on validation

enter image description here

FORM

<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>';                    
            }
    }
like image 684
Timothy Coetzee Avatar asked Jul 15 '15 10:07

Timothy Coetzee


1 Answers

You're using the text() method to set the content. This means that any HTML content found in the provided string will be encoded - as you've seen. To render the HTML in the string use the html() method instead. Try this:

.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()
});
like image 66
Rory McCrossan Avatar answered Oct 17 '22 05:10

Rory McCrossan