Re: Slashes, include, AJAX?
| From: | Rodrigo Poblanno Balp | Date: | Fri, 26 Oct 2007 06:41:33 +0000 |
| Subject: | Re: Slashes, include, AJAX? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-263686@lists.php.net to get a copy of this message | ||
Nathan Nobbe wrote:
On 10/25/07, *Rodrigo Poblanno Balp* <balpo@gmx.net <mailto:balpo@gmx.net>> wrote:Nathan Nobbe wrote:On 10/25/07, *Rodrigo Poblanno Balp* <balpo@gmx.net <mailto:balpo@gmx.net>> wrote:Hi everybody!I'm building a small ajax app which consists of only 3 php files.index.php which contains all the app and has a couple of links. When a link is clicked an Ajax request is sent to the server (using prototype) to the url central.php with only one parameter 'id'. Depending on that parameter, central.php should look for the appropiate content, in this case link1.php. Then I return a JSON object with the "things" that should be updated on index.phpSo this is the pseudo-code:index.php <html>... <body> <a href="" onclick="[send the Ajax request using prototype with the id = 1]>Link #1</a> <div id="theUpdatableDIV"></div> </body>central.php <html>... <body> <?php [read the id parameter] switch(id){ case 1: //which is the only case for now!! //build the JSON object $output = array("title" => "Link #1 Title!", "content"=>file_get_contents("link1.php"));//here I read the last file which has the content to be updated asynchronously return [the JSON object based on $output]; break; } ?> </body>link1.php <div>Welcome to the content of link #1 which was updated using <a href=" ajax.org <http://ajax.org>">ajax</a></div>the JSON object is formed ok, now, my problem is that link1.php outputs like this:<div>Welcome to the content of link #1 which was updated using <a href="ajax.org <http://ajax.org>">ajax<\/a><\/div>So at the moment I update the <div id="theUpdatableDIV"> element on index.php, I get no "real" content, but a string with the strange slashes.I've tried: stripslashes, ereg_replace, str_replace and strtr to eliminate the wierd slashes.Can anybody tell how to do it right?how are you building the json object?i recommend json_encode(utf8_encode($dataToEncode));if youre using php5. if youre using php4, try this: http://mike.teczno.com/JSON/JSON.phpsalso, you might want to check out firebug for firefox. it will let you easily analyze the interaction between the client and server.also, to verify that the json object youre generating is infact ok; run it through here: http://www.jslint.com/-nathan
Hi Nathan,
I'm using php5, and I'm building the JSON object just like that,
with the json_encode.
The problem with using utf_encode apart from the original is that
I get the \n\r for each line in the file read.
Any suggestions?
Thanxare you running windows? im guessing those are newline characters in windows format that arent getting interpreted correctly on the client side. you might try stripping out the newline characters prior to encoding the data. $jsonOut = json_encode(utf8_encode(str_replace("\r\n", '', $stringToEncode))); also, what are you trying to encode? if you are encoding arrays or objects (php that is) you will have to utf8_encode all the elements of those complex datums prior to handing them to json_encode() for the final output. -nathan Hi again Nathan, well I actually did that before, using the str_replace to eliminate al the windows \r\n, but the big problem isn't that the HTML code is being escaped too. I get something like <div>this is the content<\/div> it seems like the '/' is being escaped, but I need it as HTML. Balpo