Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i've been working on a small project that includes a simple search engine with PHP + AngularJS.

The problem is that when the keyword doesn't exists in an array it fires an error message which include HTML code. The HTML code doesn't seem to be parsed by itself, i just get plain html code.

Here's my code so far [AngularJS]

  function searchRares($scope, $http) {
  $scope.url = 'search.php';

  $scope.search = function() {

    $http.post($scope.url, { "data" : $scope.keywords}).
    success(function(data, status) {
      $scope.status = status;
      $scope.data = data;
      $scope.result = data;
    })
    .
    error(function(data, status) {
      $scope.data = data || "Bad response";
      $scope.status = status;     
    });
  };
}

My php file [search.php]

<?php
$data    = file_get_contents("php://input");
$key     = json_decode($data);

$words = ['stackoverflow', 'ask', 'answer'];

if(!in_array(strtolower($key->data), $words))
{
    echo"
    <div class=\"ui error message\">
         Not found
        </div> ";
}
else
    echo $key->data;
?>

(edit) I forgot the html code

<div class="ui raised form segment" ng-controller="searchRares">


    <div class="ui action input">
      <input type="text" ng-model="keywords" placeholder="KeyWord..">
       <div class="ui blue right labeled icon button" ng-click="search()"> Search </div>   
   </div>

<div ng-model="result">
{{result}}
</div>
</div>

Any help would be appreciated! Thanks - Eraik

share|improve this question
    
You likely should fix the error message the not be an HTML output but rather a data structure similar to what you expect for a successful response whether that be JSON, a string or whatever. –  Mike Brant Apr 30 at 20:31
    
I think i forgot to mention that im going to connect a database and if the keyword exists in a table it'll return a html DIV with data FROM the table. –  user3590817 Apr 30 at 20:35

1 Answer 1

up vote 0 down vote accepted

If I'm understanding your question correctly, you want to parse the "HTML" that you return from your PHP script using Angular.

You can do so using Angular's $apply.

That said, returning HTML is bad practice. You should return JSON instead, and then you can automatically update your $scope without needing $apply.

share|improve this answer
    
Hi sir, do you mind making an example that fits to my script? Im kind'a new to AngularJS. –  user3590817 Apr 30 at 20:33
    
This (click me) is a really nice article on using $apply. You should also make sure to check out the docs that I linked in my answer above. Honestly, I don't quite understand what you're trying to do, so I don't really know what kind of sample code to give you based on what you have. I'd seriously recommend just returning JSON instead of HTML. It'll make your life a lot easier. –  Cezary Wojcik Apr 30 at 20:37
    
Thanks for paying attention for me, why am i not returning JSON instead of HTML? Because im going to connect a database to check if the keyword exists in a table, and if it does, it'll return an DIV with the data from that table. I hope you understand me correctly now. thanks –  user3590817 Apr 30 at 20:39
    
I don't know why you're not returning JSON instead of HTML. You tell me. It sounds like you're unfamiliar with JSON. Check out the wikipedia page. The idea is that you return JSON in your PHP code using json_encode. JavaScript can then interpret the data directly. –  Cezary Wojcik Apr 30 at 20:41
    
Okay, i'll try that then, and I'm getting any trouble i'll return later. Thanks! –  user3590817 Apr 30 at 20:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.