0

This should be a very easy question, but I cannot find the answer. I have a js file that is making an ajax request to a php file. i am then trying to use the response provided by php to update the front end, but the response is not in the correct format. I am getting back the whole echo statement when I look in the console. Pardon my ignorance here but I am quite new to php. I think there is an issue sending back an a href link and br in my php

$.ajax({
    url: 'php.php',
}, function (data) {
    console.log(data);
    $('.container').html(data);
});

PHP

<?php 
    $output = 'This is<br>a<br>test<br><a href="https://stackoverflow.com"><span>Test Here</span></a>';
    echo $output;
?>

When all this has successfully worked I want to change my html to:

<div class="container">
     This is<br>a<br>test<br><a href="https://stackoverflow.com"><span>Test Here</span></a>
</div>
  • 1
    Sounds like you don't have a functioning web server configured to run php. See http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page – Sam Dufel Aug 31 '16 at 15:35

1 Answers1

0

When using jQuery's $.ajax function you can use either of { success: function() {...} } or .done() promise:

$.ajax({
    url: 'php.php',
    success:  function (data) {
        console.log(data);
        $('.container').html(data);
    }
});

Or

$.ajax({
    url: 'php.php',
    success:  function (data) {
        console.log(data);
        $('.container').html(data);
    }
}).done(function( data) {
    console.log(data);
    $('.container').html(data);
});
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • I think the issue is with the php file, no? Not sure if it is returning the data as I want –  Aug 31 '16 at 14:34
  • Even if your PHP code returned the correct data - the jquery code wouldn't work. BTW, you should also add the `jquery` tag to your question. – Dekel Aug 31 '16 at 14:37
  • I think there are issues sending a link back in the php –  Aug 31 '16 at 14:38
  • Regarding the php code - you can check the request and response in the developers tool bar. – Dekel Aug 31 '16 at 14:38
  • No, there is no issue there. It's a normal string, you can do this. – Dekel Aug 31 '16 at 14:38
  • i have checked the response in the network tools and it is showing `` –  Aug 31 '16 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122317/discussion-between-dekel-and-phantom). – Dekel Aug 31 '16 at 14:40