Viewed   90 times

I have encountered a similar problem described here (and in other places) - where as on an ajax callback I get a xmlhttp.responseText that seems ok (when I alert it - it shows the right text) - but when using an 'if' statement to compare it to the string - it returns false.

(I am also the one who wrote the server-side code returning that string) - after much studying the string - I've discovered that the string had an "invisible character" as its first character. A character that was not shown. If I copied it to Notepad - then deleted the first character - it won't delete until pressing Delete again.

I did a charCodeAt(0) for the returned string in xmlhttp.responseText. And it returned 65279.

Googling it reveals that it is some sort of a UTF-8 control character that is supposed to set "big-endian" or "small-endian" encoding.

So, now I know the cause of the problem - but... why does that character is being echoed? In the source php I simply use

echo 'the string'...

and it apparently somehow outputs [chr(65279)]the string...

Why? And how can I avoid it?

 Answers

2

To conclude, and specify the solution:

Windows Notepad adds the BOM character (the 3 bytes: EF BB BF) to files saved with utf-8 encoding.

PHP doesn't seem to be bothered by it - unless you include one php file into another - then things get messy and strings gets displayed with character(65279) prepended to them.

You can edit the file with another text editor such as Notepad++ and use the encoding
"Encode in UTF-8 without BOM",
and this seems to fix the problem.

Also, you can save the other php file with ANSI encoding in notepad - and this also seem to work (that is, in case you actually don't use any extended characters in the file, I guess...)

Tuesday, October 25, 2022
2

Your selection is fine, but you need to store result into an variable like:

$count = $db->querySingle("SELECT COUNT(*) as count FROM Podcast_Export WHERE House_number = '".$value."'"); 
if($count <= 0){ 
    $db->exec("INSERT INTO Podcast_Export (House_number,Status) 
               VALUES ('".$value."', 'Go')");
}

Using querySingle to get the no of rows in SQLite.

note that, i have chanaged the House# to House_number here and removed AND status = 'GO' clause as all of them having GO status.

Friday, September 16, 2022
 
opello
 
5

i use alot ajax and always use only utf8 and never had a problem , i'm mostly on chrome and safari ios

maybe try changing ur ajax script by removing all the headers.and using the new FormData (should work on most modern browsers) ... sure not on all.

document.forms[0] means it takes all the fields from first form in your page. else give it an id and call document.getElementById('myform') i also don't use anymore readyState... onload

to return the response in postresponsefunction u write this.response

var fd=new FormData(document.forms[0]),
c=new XMLHttpRequest();
c.open('POST',strURL);
c.onload=postresponsefunction;
c.send(fd);

here is a complet working php script with post & ajax file is named 'test.php'

<?php
if($_REQUEST){
print_r($_REQUEST);
}else{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>postajax</title>
<script>
var 
rf=function(){
 document.getElementsByTagName('div')[0].innerText=this.response;
},
sf=function(e){
 e.preventDefault();e.stopPropagation();
 var fd=new FormData(document.getElementsByTagName('form')[0]),
 c=new XMLHttpRequest();
 c.open('POST','test.php');
 c.onload=rf;
 c.send(fd);
};
window.onload=function(){
 document.getElementsByTagName('form')[0].onsubmit=sf;
}
</script>
</head>
<body>
<form>
<input type="text" name="mytext"><input type="submit" value="submit">
</form><div></div>
</body>
</html>
<?php
}
?>

ie

if (!window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    return new ActiveXObject(”Microsoft.XMLHTTP”);
  };
}
Saturday, October 22, 2022
5

The array key is encoded in UTF-8 if it indeed comes as UTF-8 string from the database. Apparently your source code file is not encoded in UTF-8, I'd guess it's encoded in Latin-1. A comparison between a UTF-8 byte sequence and a Latin-1 byte sequence is therefore unsuccessful. Save you source code files in UTF-8 and it should work (consult your text editor).

Wednesday, December 7, 2022
 
ihtcboy
 
5

You need to authenticate the user somehow.

Your user needs to be authenticated with a username and a password.

PHP session can be used to remember, and you should use a database table or a text file on the server to store file ownership information.

Then, before unlinking anything, your logic should make sure that the currently "authenticated" user is the owner of the file.

Tuesday, September 27, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :