PDA

View Full Version : Text Parsing


phil_b
11-15-2009, 11:33 PM
Currently i have tabs that when changed load (AJAX) different data from MySQL. Do I need to run this through a parser when being displayed on the page? For example the first symbol below is displayed correctly but the second is not recognised? This does not happen when the data is displayed without AJAX... Do I need to prepare data sent using AJAX differently than usual GET and POST requests?

-



Thanks, Phil

phil_b
11-16-2009, 03:40 PM
hi, can anyone assist with this? I add a header with the correct charset and it does not recognise this character (big dash) but recognises all other characters.

Any pointers would be appreciated on how to format text pulled in via AJAX...

ckeyrouz
11-16-2009, 05:36 PM
Please post more details and a bit of code to be able to help you.

phil_b
11-16-2009, 10:20 PM
Please post more details and a bit of code to be able to help you.

sure no problem. To explain: I call init() which sets up my tab events. Then when a tab is clicked it runs initialiseContent() which fires off an event with sendrequest() to my PHP file. The rest is just minor formatting for the chosen tab etc.

My problem is that the review when returned via AJAX does not recognise one character while when returned normally on the page it does. See below - the 4th comment is a passage from the above review which does display the character:

http://tinyurl.com/ygwznd5

Let me know if you need any more info, any advise appreciated :)

var reviews = new reviews();
var xmlhttp;
var classArray = Array('comment', 'review', 'video', 'stats');

function reviews(){

/*--------------------------------------------*/
// Setup tab events
/*--------------------------------------------*/

this.init = function()
{
for(key in classArray)
{
var contentDiv = document.getElementById( 'pp-content-tab-' + classArray[key] );

contentDiv.style.cursor = 'pointer';
contentDiv.onclick = this.initialiseContent;
}
}

/*--------------------------------------------*/
// Initialise and populate tab
/*--------------------------------------------*/

this.initialiseContent = function( tab )
{
if( this.id )
{
var tab = this.id.substring( 15 );
}

if( !in_array( tab, classArray ) )
{
var tab = 'comment';
}

for(key in classArray)
{
if( classArray[key] == tab )
{
document.getElementById( 'pp-content-tab-' + classArray[key] ).className = 'pp-tabon';

} else {

document.getElementById( 'pp-content-tab-' + classArray[key] ).className = 'pp-taboff';
}
}

sendrequest( tab );
}

/*--------------------------------------------*/
// xmlhttp logic
/*--------------------------------------------*/

function sendrequest( eventChoice )
{

xmlhttp = GetXmlHttpObject();

if ( xmlhttp == null )
{
alert ("Browser does not support HTTP Request");
return;
}

if ( !eventChoice || eventChoice == '' )
{
// no value so return
return;
}

var url = "./?page=loadtab";

url = url + "&q=" + eventChoice + "&r=" + reviewID;
url = url + "&sid=" + Math.random();

xmlhttp.onreadystatechange = stateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=iso-8859-1");
xmlhttp.send( null );
}

function GetXmlHttpObject()
{
if ( window.XMLHttpRequest )
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if ( window.ActiveXObject )
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}

return null;
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById('pp-main-tab-content').innerHTML = xmlhttp.responseText;
$('#bbcode').markItUp(myBbcodeSettings);
}
}

/*--------------------------------------------*/
// Misc
/*--------------------------------------------*/

function in_array( needle, haystack )
{
for (key in haystack) {

if (haystack[key] == needle) {

return true;

}

}
}

}

phil_b
11-16-2009, 11:43 PM
to add to the above, if I add the below above where I echo the tab content it works:

header("Content-Type: text/html; charset=Windows-1256\n");

But using the below (which is my standard charset does not:

header("Content-Type: text/html; charset=iso-8859-1\n");

Narrowing down the issue but strange my original char set does not work... Any tips on this?

rnd me
11-17-2009, 12:23 AM
to add to the above, if I add the below above where I echo the tab content it works:

header("Content-Type: text/html; charset=Windows-1256\n");

But using the below (which is my standard charset does not:

header("Content-Type: text/html; charset=iso-8859-1\n");

Narrowing down the issue but strange my original char set does not work... Any tips on this?

use utf8 for everything...

phil_b
11-17-2009, 08:33 AM
changed the page to utf-8 and it now does not even recognise the small dash on the non AJAX page....

ckeyrouz
11-17-2009, 05:35 PM
replace this line :
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=iso-8859-1");


with this line:
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=utf-8");


you are sending the request with iso encoding while it should be utf-8.

Try it and check the result please.

phil_b
11-17-2009, 10:57 PM
replace this line :
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=iso-8859-1");


with this line:
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=utf-8");


you are sending the request with iso encoding while it should be utf-8.

Try it and check the result please.

i would need to convert my current data which isnt possible currently, I have a forum with millions of posts.

But on my site the Review displays fine with iso, its only when I display via AJAX the data that it has problems. See http://tinyurl.com/ygwznd5

Top review = OK
Scroll down to the tabs and select Reviews - Exact same review from the same table cell does not display correctly, strange?