Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
6.1k views
in Plugins by
edited by

I am developing a new plugin "event-watcher-ajax" that is supposed to display incoming events life in a widget and on a page.

Now I try to ajax-call the page, however I get this error:

PHP Fatal error:  Can't use function return value in write context in /event-watcher-ajax/qa-event-watcher-page.php on line 64
 

CODE of the page:

        function process_request($request)
        {
         // we received post data, so it should be the ajax call!
         if( qa_post_text('request') ) {
             // process and return
             // the return causes the error above!
         }
      else {

        // default loading of page
        // preparation of output and stuff...

        // + jquery ajax call "$('#responsecontainer').load('".qa_self_html()."')...
       }

---

Update: I am reading the post just once and it works: $transferString = qa_post_text('request');

But another problem occured: The ajax call to the plugin itself outputs all theme content (menu and stuff), even if I catch the post first and do not output $qa_content = qa_content_prepare();

Do I have to call a layer? What am I doing wrong?
Any help appreciated!

 

1 Answer

0 votes
by
selected by
 
Best answer

It's difficult to say without the actual code, but if you just want to output some basic text on the AJAX call, all you need to do is echo it directly then return. In your if statement, something like this:

echo 'The output';
return;

Later on in the function you need to have this somewhere:

$qa_content = qa_content_prepare();
$qa_content['title'] = 'Page title';
// ...
return $qa_content;

Also, which line is line 64? That might help you identify the exact error.

by
Dear Scott, thank you very much!

Line 64 is:
$lasthours = (isset(qa_post_text('request'))) ? qa_post_text('request') : 8;

Why is that a "write context"?
by
Well, now I am reading the post just once and it works:
$transferString = qa_post_text('request');

But another problem occured: The ajax call to the plugin itself outputs all theme content (menu and stuff), even if I catch the post first and do not output $qa_content = qa_content_prepare();

Do you mind if I send you the code to take a look?
by
Ah OK I think it's because you are not passing a variable into "isset". isset isn't an actual PHP function even though it looks like it (similar to echo, print and others). You should do this:

$transferString = qa_post_text('request');
$lasthours = empty($transferString) ? 8 : $transferString;

(I used empty here because IIRC isset would always return true as the variable has been set, even if to null.)
by
Thanks for the feedback. The only issue that remains: Why is the ajax call receiving the entire theme setup and not only the lines I echo? ...
by
If you return null from the process_request() function of a page module, the page should be empty. You can see an example of this in the opensearch-support plugin that comes with Q2A.
by
I do the same with my chat room plugin too.
by
It works, awesome!

Problem was actually me. I used: $transferString = qa_post_text('request'); → but instead of "request" I was naming the param "hours". Changing this goes into my if($transferString !== null) ... and returns NULL.

Thank you both, great job!
...