Jump to content

Best practice for handling GET variables in Ajax Endpoint?


Stefanowitsch
 Share

Recommended Posts

Hi @bernhard

While testing my ajax endpoint with RockFrontend I noticed a behaviour. This is more a ProcessWire related theme however, there is nothing wrong with the ajax endpoint feature itself.

I am using the ajax endpoints to handle form submissions on my pages.

The URL for a request looks like this:

/ajax/form?pageId=1401&formId=1418&lang=1016

So I am passing three get variables here:

1. pageId 
2. formId
3. lang 

Inside my endpoint php file I am passing those values with the files->render method to handle the form submission:

echo $files->render('elements/_formular',['page' => $pages->get($pageId), 'form' => $pages->get($formId), 'lang' => $lang]);

As long as I am calling the endpoint URL together with those parameters, everything is fine.

If I just call:

/ajax/form

I receive a PHP error that one ore more of the variables are "NULL". This is obviously!

Now comes the part that I don't really get:

I am retreiving the GET values like this via the ProcessWire API:

$pageId = $input->get('pageId','int');

But this variable returns two different values, depending on how I output them:

bd($pageId); // returns "0"

return $pageId // returns ""

So to check if I am actually passing values via GET I use the following condition: 

if (isset($pageId, $formId, $lang)) {
    echo $files->render('elements/_formular',['page' => $pages->get($pageId), 'form' => $pages->get($formId), 'lang' => $lang]);
}

But this does not work, the variables are all set to "0" when I am passing no parameter (I would expect NULL).

So the question is: How would you elegantly check of one of those variables are set?

Link to comment
Share on other sites

Hi,

5 hours ago, Stefanowitsch said:

But this does not work, the variables are all set to "0" when I am passing no parameter (I would expect NULL).

$input->get('pageId','int'); is casting the value to int, so null becomes zero. This is the equivalent of $sanitizer->int(null);

  • Like 2
Link to comment
Share on other sites

48 minutes ago, da² said:

Hi,

$input->get('pageId','int'); is casting the value to int, so null becomes zero. This is the equivalent of $sanitizer->int(null);

Thanks! Now I understand what was happening. Removing the "int" argument let's me check via "isset" if any of those parameters are passed.

  • Like 1
Link to comment
Share on other sites

If you don't like to use zero as an "empty" value, you can ask for a default value:

$input->get('pageId', 'int', -1);

But zero is fine since there's no zero ID or zero lang and we can check value easily.

$pageId = $input->get('pageId','int');
if ($pageId) ...

 

Edited by da²
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...