Click to See Complete Forum and Search --> : ? Validation and UNICODE Problems


Synetech
June 20th, 2007, 03:49 PM
Hi, I’m working on a little AJAX app using a PHP script but am having two problems with it:


(1) My XHTML file won’t validate because of this part:


XMLHttp.send(
"action=view"
+(form.title.value == "" ? "" : "&title=" +form.title.value)
+(form.artist.value == "" ? "" : "&artist="+form.artist.value)
+(form.album.value == "" ? "" : "&album=" +form.album.value)
+(form.year.value == "" ? "" : "&year=" +form.year.value)
);
The validators complain about the ampersands; they say that the entities &title, &artists, &album, and &year are not valid. I tried replacing them with &title, etc. and that does fix the validation, but they no longer register as a parameters in the PHP script.

Any ideas?


(2) The PHP script returns some text that is inserted into a DIV on the page from XMLHttp.responseText, but it gets messed up if the returned text has UNICODE characters in it.

Any ideas?


Thanks a lot.

PeejAvery
June 20th, 2007, 04:07 PM
You are missing an & behind the "view."

You really should break up that send command. Try the following.
var theTitle = form.title.value;
var theArtist = form.artist.value;
var theAlbum = form.album.value;
var theYear = form.year.value;

var parameters = "action=view&title=" + theTitle + "&artist=" + theArtist + "&album=" + theAlbum + "&year=" + theYear;

XMLHttp.send(parameters);

Synetech
June 20th, 2007, 04:19 PM
“Behind the view”? You didn’t put one behind the view either (and I’m pretty sure it isn’t necessary, possibly even incorrect).


In any case, I tried your code and it still doesn’t validate; it gives the same errors: “Warning: cannot generate system identifier for general entity "title"”
“Error: general entity "title" not defined and no default entity”
“Error: reference to entity "title" for which no system identifier could be generated”

PeejAvery
June 20th, 2007, 04:22 PM
No. It is not incorrect. To separate parameters you must use &. When I said behind the view I was talking about...action=view. But I see that I wasn't looking far enough down the code.

Are you sure you have all the AJAX POST headers set up?

You should have all the following.
obj.open('POST', url, true);
obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
obj.setRequestHeader('Content-length', parameters.length);
obj.setRequestHeader('Connection', 'close');
obj.send(parameters);

Synetech
June 20th, 2007, 04:32 PM
No. It is not incorrect. To separate parameters you must use &. When I said behind the view I was talking about...action=view. But I see that I wasn't looking far enough down the code.Oh ok, I thought you meant ?action=view, which I’m fairly sure would be correct because ampersands separate parameters and you can’t put one before the first one.

Are you sure you have all the AJAX POST headers set up?

You should have all the following.
obj.open('POST', url, true);
obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
obj.setRequestHeader('Content-length', parameters.length);
obj.setRequestHeader('Connection', 'close');
obj.send(parameters);

Pretty sure, I’ve gotXMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

XMLHttp.setRequestHeader("Charset", "UTF-8"); //this was an attempt to fix problem (2); it didn’t help


The first line was all that’s listed in the AJAX texts I got it from.

Problem (1) isn’t an XMLHttpRequest problem anyway, it’s a markup problem. The XHTML validators complain about the presence of an ampersand in the string, but they are necessary to pass multiple parameters.

Also, any reason you set the open command to synchronous mode?

PeejAvery
June 20th, 2007, 05:06 PM
Problem (1) isn’t an XMLHttpRequest problem anyway, it’s a markup problem. The XHTML validators complain about the presence of an ampersand in the string, but they are necessary to pass multiple parameters.
Yes, the markup might be part of the problem, but you said that my example did not work either. That has no markup issues. Can you post the whole page's code?

Also, any reason you set the open command to synchronous mode?
That is just a snippet I had from another piece of code. I used synchronous for control purposes in that script.

Synetech
June 20th, 2007, 05:28 PM
Here’s a really basic page that has the problem:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Problem!!!</title>
<script type="text/JavaScript">
//validators complain about this, but you can’t replace the ampersands with &amp;
var postparams="param1=value1&param2=value2";
</script>
</head>
<body>
</body>
</html>

PeejAvery
June 20th, 2007, 05:32 PM
Change...
<script type="text/JavaScript">
To...
<script type="text/javascript">

Synetech
June 20th, 2007, 05:46 PM
Actually, I fixed problem (1). From the W3C spec for XHTML (http://www.w3.org/TR/xhtml1/#h-4.8):

<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>


Apparently UNICODE characters in JavaScript comments don’t validate as valid SGML. :confused:

Now, I just need to figure out why the PHP response isn’t UTF-8.

PeejAvery
June 20th, 2007, 05:49 PM
PHP response? utf8_encode() (http://us.php.net/manual/en/function.utf8-encode.php) maybe?

Synetech
June 20th, 2007, 06:23 PM
PHP response? utf8_encode() (http://us.php.net/manual/en/function.utf8-encode.php) maybe?

Yup, that worked. The “trick” was figuring out where to use it. Also, only part of it was fixed, but a quick test revealed that the part that still isn’t working is because somehow, the data was originally inserted into the database incorrectly (doesn’t really matter though since it was just a temp table for testing).

They both seem to be fixed now. Thanks.

However, it’s annoying that you can’t put any UNICODE characters in the HTML code or in JavaScript strings, you have to encode them which is not convenient.

Synetech
June 20th, 2007, 06:25 PM
PHP response? utf8_encode() (http://us.php.net/manual/en/function.utf8-encode.php) maybe?

Yup, that worked. The “trick” was figuring out where to use it. Also, only part of it was fixed, but a quick test revealed that the part that still isn’t working is because somehow, the data was originally inserted into the database incorrectly (doesn’t really matter though since it was just a temp table for testing).

They both seem to be fixed now. Thanks.

However, it’s annoying that you can’t put any UNICODE characters in the HTML code, you have to encode them.

PeejAvery
June 20th, 2007, 06:28 PM
Glad your problems are fixed.