Re: Slashes, include, AJAX?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/25/07, Rodrigo Poblanno Balp <balpo@xxxxxxx> wrote:
>
>  Nathan Nobbe wrote:
>
> On 10/25/07, Rodrigo Poblanno Balp <balpo@xxxxxxx> 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.php
> >
> > So 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">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">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.phps
>
> also, 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?
>
> Thanx
>

are 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux