Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Php code for ajax encoding

1 view
Skip to first unread message

GarryJones

unread,
Sep 5, 2010, 12:49:19 PM9/5/10
to
I am trying to retrieve text from the my database using ajax code
witten in php.

Somewhere its getting twisted. All the text is retrieved and displayed
correctly except the Swedish characters used in some of the words that
are stored in my mysql phpmyadmin database.

These are displayed seriously incorrectly. Each character deletes the
following 2 characters. Så "påskvecka" becmes p*vecka where * is an
upright diamond with a question mark in it. This offending character
can not be copied into this newsgroup posting, it just wont take it.
Everything else on the page is correctly (Swedish characters written
elsewhere ARE displayed correctly, the problem ONLY applies to the
ones retrieved with php code via ajax from the server.

So what is the php code to turn the correctly stored characters in the
mysql phpmyadmin database back into the correct ones before displaying
junk.

Marious Barrier

unread,
Sep 5, 2010, 12:54:10 PM9/5/10
to

GarryJones

unread,
Sep 5, 2010, 1:55:47 PM9/5/10
to

> Try with this.-https://developer.mozilla.org/en/XMLHttpRequest#overrideMimeType%28%29- Hide quoted text -

Not quite understandning that....

This is the code from my php file....

var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for Firefox, Opera, IE7, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("POST",url,true);
xmlhttp.send(null);
}
else
{
alert("Använd knapparna längst ner på sidan");
}
}

function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('T1').innerHTML=xmlhttp.responseText;
}
else
{
alert("Problem med datahämtning:" + xmlhttp.statusText);
}
}
}

Where do I paste in your answer to that code?

Garry Jones
Sweden

Marious Barrier

unread,
Sep 5, 2010, 2:16:08 PM9/5/10
to
On 09/05/2010 01:55 PM, GarryJones wrote:
> Where do I paste in your answer to that code?

Ah, what you are looking for is someone to do the job for you? :S

Just override the mime-type before sending the query to the server.

Jerry Stuckle

unread,
Sep 5, 2010, 2:22:54 PM9/5/10
to
On 9/5/2010 1:55 PM, GarryJones wrote:
>
>> Try with this.-https://developer.mozilla.org/en/XMLHttpRequest#overrideMimeType%28%29- Hide quoted text -
>
> Not quite understandning that....
>
> This is the code from my php file....
>
<snip javascript>

>
> Where do I paste in your answer to that code?
>
> Garry Jones
> Sweden
>

The first thing you need to understand is the difference between PHP and
Javascript. PHP runs on the server; the javascript code you posted runs
on the client.

Try comp.lang.javascript for your javascript questions.

If you have PHP code to discuss, this is the correct place. But you
haven't shown us anything indicating whether or not this is a PHP
problem yet.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

GarryJones

unread,
Sep 5, 2010, 2:53:20 PM9/5/10
to
Hi again

I changed THIS


{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("POST",url,true);
xmlhttp.send(null);
}

TO THIS

{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("POST",url,true);
void overrideMimeType(
in AUTF8String ('text/xml; charset=iso-8859-1');
xmlhttp.send(null);
}

But no luck, I don't actually understand much of this syntax. I
realise also I may be in the wrong newsgroup, not all files that end
in .php are php problems but I thought there might be so php code to
sort this problem out.

I'd be enternally greatful if someone could push me (hard) in the
right direction...

Garry Jones
Sweden

Marious Barrier

unread,
Sep 5, 2010, 2:57:22 PM9/5/10
to
On 09/05/2010 02:53 PM, GarryJones wrote:
> void overrideMimeType(
> in AUTF8String ('text/xml; charset=iso-8859-1');

You should do it this way.-
xmlhttp.overrideMimeType('text/html; charset=UTF-8');

(assuming you are retrieving html data in UTF-8)

> I'd be enternally greatful if someone could push me (hard) in the
> right direction...

Well, lets hope this works.

GarryJones

unread,
Sep 5, 2010, 3:47:06 PM9/5/10
to
> Well, lets hope this works.

I tried this..
xmlhttp.onreadystatechange=state_Change;
xmlhttp.overrideMimeType('text/html; charset=UTF-8');
xmlhttp.open("POST",url,true);
xmlhttp.send(null);

Still does not work.

However the people in javascript showed me how to do this with php.

I convert the data retrieved from the server with ICONV to UTF-8.

For instance:

$resmalid=iconv("ISO-8859-1", "UTF-8", $b4resmalid);

This works with server and user entered data but for the raw display
data with Swedish characters I have on the pages I construck I have to
use the codes, so its only a work around but it solves the problem. So
a php solution works for the main problem....

Marious Barrier

unread,
Sep 5, 2010, 4:07:11 PM9/5/10
to

Please post an URL where I can see the output from the PHP file.

Jerry Stuckle

unread,
Sep 5, 2010, 8:03:20 PM9/5/10
to

Post in an appropriate newsgroup and you'll get good answers. Continue
to post in the wrong newsgroup and you'll continue to get crap answers,
like you have so far.

Jerry Stuckle

unread,
Sep 5, 2010, 8:04:42 PM9/5/10
to

It would be much better if he posted this to an appropriate newsgroup
such as comp.lang.javascript - where he will get GOOD advice.

This is a PHP newsgroup (hint: that's why the "php" in it's name), not a
javascript newsgroup. Please learn the difference.

"Álvaro G. Vicario"

unread,
Sep 6, 2010, 2:43:14 AM9/6/10
to

Internally, computers can only handle numbers. So when you need to use
text you must use a translation table to convert each individual letter
into a number. Such table is called "character set" (or charset) and
there're many to choose from. The first step is to pick a charset that's
suitable for Swedish words and make sure all your applications (MySQL,
PHP, JavaScript, HTML...) are using it properly.

I've found this article very illustrative:

http://www.joelonsoftware.com/articles/Unicode.html

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

B.r.K.o.N.j.A.

unread,
Sep 9, 2010, 5:56:48 AM9/9/10
to
Just add following line in your php script that generates content that
you're retrieving using ajax. All you need (and this code snippet does it)
to do is to set appropriate charset/encoding so the browser knows what
content encoding it's getting back.

header( 'Content-type: text/html; Charset: utf-8');

(im typing this from the top of my head so syntax is probably wrong :) )

Brko


0 new messages