0

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?

2
  • What you have is the behaviour from AJAX - especially jQuery, anything printed (including html) in the php script will be parsed and returned by the AJAX. To get only certain data, split up your php script into parts, style, html etc. Commented Oct 14, 2015 at 13:16
  • i got that thank you friend Commented Oct 14, 2015 at 13:22

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

@JeroenBellemans depends on how the function should react if called again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.