Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

AJAX and maybe stupid php question...


Go to End


3 Posts   9443 Views

Avatar
zyko

Community Member, 66 Posts

18 May 2008 at 12:00am

Edited: 18/05/2008 12:22am

Hi

sorry for that maybe stupid question.
i 've read
http://doc.silverstripe.com/doku.php?id=recipes:ajax_basics
but don't understand this thing totally.
and i didn't do any ajax things before, so i'm a rookie in this area.

(1) what ist this
Director::is_ajax()
call for?

(2) i have some client-script code not originating from silverstripe that want's to be connected to same ajaxified controller functions.
is there any additionally need to do on a controller-function to act as some client-script expects it to be a ajax-pendant?
(must there be some protocol-header set or anything else)
do i have to include this
Requirements::javascript('mysite/javascript/jquery.js');
if i don't do things for silverstripe ajaxified controls?

let's say, that if i don't need any template rendering things.
so i thought the simplest solution was to return the strings i need.

fe:

class  mycontroller  xxx
  function getSomethingsContent() {
    $ds = DataObject::get(yyy...);
    return "<div id = 'blabla'>$ds->Content<div>";
  }

is this a good solution or not?

if i call this thing in the webbrowser via url it seems to be ok.
if i call it out of a javascript to replace someones innterhtml i have problems
if this thing hase special HTML characters.
they come as XML where they should be HTML... which leeds to javascript-errors
what i don't understand is, what system-part does this xml-translation thing, if i don't use that template rendering mechanism?
Do i have to call another function to encode to another format?

(3) now for the stupid php question:
i've seen this thing sometimes in code, but can't the hell figure out what it does.
(try to search for <<< in google. aaarghhh...)

$ret = <<<HTML
<div id="Form_ResultForm">
{$form->renderWith("Form")}
</div>
HTML;

i think it does something like format it ready for HTML.
but i'm not sure, might also only be some php-syntax construct to allow many lines
of text to be entered in a friendly way.

Avatar
Grayzag (aka ajshort)

29 Posts

18 May 2008 at 1:57pm

Edited: 18/05/2008 1:58pm

Hi Zyko,

in response to your questions: Director::is_ajax() is used to tell if the SilverStripe instance running has been called by an ajax request, rather than just a normal HTTP request. With the <<< thing, it means that you can specify a custom endpoint for your string, that is instead of:

$string = "bla";

you can use:

$string = <<<bla
this is all
the string contents
bla;

Basically it tells PHP to keep reading the string in until it finds an instance of bla at the beginning of a line (or whatever delimeter you specify after <<<).

As for the special characters, im not 100% sure, but maybe you could try replacing the return with an echo (not a good coding practice - but just as a test) to see if that works.

Avatar
zyko

Community Member, 66 Posts

18 May 2008 at 9:00pm

Edited: 18/05/2008 11:23pm

Hi Grayzag

THX for (3) :-))) makes things clearer for me
(1) would be interesting to me, how this was implemented.
Are there other protocols used, where they take out this info?
but might look myself in code to find out ;-)

(2). I'd some problems understanding the sample in
http://doc.silverstripe.com/doku.php?id=recipes:ajax_basics
the most confusing part for me was that the index-function uses

  return $this->renderWith("ajaxSnippet");

where i expected to see this thing happen in an usual function.
it seems to show some image that has been uploaded,
where me is missing any image-upload code???
for the xml-thing. i think i'll try rendering via snippet, as shown in the sample.
is maybe a better solution then have things in code...
[UPDATE]
addon to my 'javascript' error.
my client-script sets xxx.innerHTML = s;
where it gets s from my controller-ajaxified-function.
this produces an javascript error (in Firefox)
An invalid or illegal string was specified

the funny thing is that actually me using
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

.innerHTML should not be valid to use
if you look at
http://www.webdeveloper.com/forum/showthread.php?s=b1e88d910c127646d403a781adc14742&t=178347&page=2
they say someone shall use: createElementNS()
But it seems to work, if there are no special characters inside. only with bad chars fe.
&szlig;
i get this script-error in firefox(maybe in ie too?)

AAARGHHH...
wondering how silverstripe is doing this (have to look at JQuery)...

g
Helmut