Question

I have modeled an AJAX search after this example a long time ago. I have changed some things - the whole request process is handled now via jquery. But the PHP file which is requested is basically the same. I have added an header for multibyte compatibilty...

header("content-type: text/html; charset=utf-8");

but other than that: little edits. And it worked quite well. Until recently: I wanted to switch to strict XHTML and have been transforming my html code. And I mean strict: I have even changed the MIME type to "application/xhtml+xml". The only problem: When switching to this type, the AJAX search isn't working anymore. The problem is not jquery, it is PHP's DOMDocument class. Here is the central simplified portion of the PHP file, which is requested via jquery

if (mb_strlen($searchTerm) > 2) {

  $xmlDoc = new DOMDocument('1.0', 'UTF-8');

  $xmlDoc->load('data.xml');

  echo 'Results:<br />';

  $x = $xmlDoc->getElementsByTagName('data');

  for($i=0; $i<($x->length); $i++) {

    $name=$x->item($i)->getElementsByTagName('name');

    if ($name->item(0)->nodeType==1) {

      $name = $name->item(0)->childNodes->item(0)->nodeValue;

      if (mb_stristr($name,$searchTerm)) echo $name .'<br />';

    }

  }

} else {

  echo 'Searchterm to short';

}

The problem lies not with jquery, because, when I input 2 letters into the input field which triggers the AJAX request, the returned text is 'Searchterm to short' (so request is basically wokring). Now comes the confusing part: When I type a word, which should be found in the XML file, nothing is returned: no error, no text, no nothing. When I type a word which isn't in the XML file, at least 'Results:
' is returned. So it seems: PHP is looping through the XML file somehow. When I switch with the whole project to the content type "header('content-type: text/html; charset=utf-8')" everything is working fine again. Do I have to change the mime type of the requested PHP file, too - I have already tried different variations without success. Do I have to change the DOMdocument object somehow to get it working in a strict XHTML enviroment...?

Was it helpful?

Solution

I'm quite embarrassed. The AJAX request output breached the strict XHTML rules. Just haven't seen it. The example above would run without problems, because I've stripped some of the flawed HTML code. However: problem solved, own stupidity confirmed...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top