Mohsin Ali Shah
Mohsin Ali Shah

Reputation: 3

jquery ajax call back request bringing html from php file

I am making an ajax request where I will type name in input box to search data from db, It's working fine,

<script>

    $(document).ready(function(){   
    $('input#name-submit').on('click', function() {

    var nameVar = $('input#name').val();        
    if($.trim(nameVar) != ''){
        $.post('records.php', {name: nameVar}, function(data){
        $('div#name-data').text(data);
});    
    }    
});
});

</script>

----php

if(isset($_POST['name'])){    
$name = $_POST['name'];     
echo $name;

Now the problem is when I embed the html into php and echo it, It prints the html also

echo  "<div class='tblDiv'>";
echo  "</div>";

result: <div class='tblDiv'> Name </div> 

how to avoid that and get the result in style?

Upvotes: 0

Views: 67

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388416

You need to use .html() instead of .text()

 $('#name-data').html(data);

.text() will not parse the html content, it will escape the html and print the passed value as it is

Upvotes: 4

Related Questions