Community

Snippet return $variable not working

In the following code im trying to return formit form with a custom filepath from my database. If i add the path in the formit form like this &ajaxuploadTarget=$SingleField no images uploaded to this path, but if i add the path harcoded like this “/path/to/images/” it works and uploads new images in that folder.

I have realized that modx does not allows you to return $variables in a snippet. There must be another way to do it. basically i want to return a variable from a snippet.

    <?php
$uid = $modx->getLoginUserId();
$formid = $_GET["formid"];
$uid = $_GET["userid"];
$user = $modx->getLoginUserName();
global $SingleField;
if($user){
    $sql = "SELECT * FROM modx_formit_forms WHERE id='$formid'";
    foreach ($modx->query($sql) as $row) {
        $values= $row['values'];
    }
    if (!is_object($modx->query($sql))) {
      return 'No result!';
    }
    else {
        $values=json_decode($values);
        $values_to_array = (array) $values;
        $SingleField = $values_to_array["image"];
        if(strpos($SingleField, ",") !== false ){//if there are multiple images
            $SingleField = explode(',', $SingleField, -1);
              $SingleField = $SingleField[0];
            $path=pathinfo($SingleField)['dirname'];
            $SingleField = substr($path, strpos($path, '/') + 8);
        }else{//if there is a single image
            $SingleField = str_replace("/assets/", "", $SingleField);
            $SingleField=pathinfo($SingleField)['dirname'];
        }
    }
}
return "[[!FormIt? 
     &hooks=`AjaxUpload2Formit, HandleFormForUpdate`
     &preHooks=`Formit2AjaxUpload`
     &ajaxuploadFieldname=`image`
     &ajaxuploadTarget=`$SingleField`
     &ajaxuploadUid=`image`
     &ajaxuploadClearQueue=`1`
 ]]";

That looks syntactically correct to me. You can try wrapping the variable with curly braces to better indicate where the variable goes like this:

return "[[!FormIt? 
     &hooks=`AjaxUpload2Formit, HandleFormForUpdate`
     &preHooks=`Formit2AjaxUpload`
     &ajaxuploadFieldname=`image`
     &ajaxuploadTarget=`{$SingleField}`
     &ajaxuploadUid=`image`
     &ajaxuploadClearQueue=`1`
 ]]";

but it should be working regardless.

Are you sure your query returns the appropriate results and the resulting Formit call is right? For testing, you can try using echo instead of return, following it with exit(0); to see the exact output before MODX parses it further:

echo "[[!FormIt? 
     &hooks=`AjaxUpload2Formit, HandleFormForUpdate`
     &preHooks=`Formit2AjaxUpload`
     &ajaxuploadFieldname=`image`
     &ajaxuploadTarget=`$SingleField`
     &ajaxuploadUid=`image`
     &ajaxuploadClearQueue=`1`
 ]]";
exit(0);

(Normally you would always use return from a snippet, but for a quick debug that quits processing, you need to echo and exit.)


Some notes though.

Point 1.

That code is vulnerable to SQL injections. Never ever ever use a $_GET directly in a query. xPDO will try to protect you, but $modx->query bypasses most of those protections.

At the very least, use $modx->quote($formid) before inserting it into the query, but better yet, use standard xPDO methods to retrieve data which use prepared statements that are much safer.

Point 2:

$values=json_decode($values);
$values_to_array = (array) $values;

Just use $values = json_decode($values, true) which will return an associative array instead of an object.

Point 3. Don’t use globals (global $SingleField;) Your code doesn’t need it.

Point 4. I don’t know your full use case for the dynamic upload target and the way you’re basing it off an existing form submission, nor do I know if/how much AjaxUpload protects it, but you may want to make sure it’s always a valid path in a section you define. Validate it before returning it.

Thank you for your reply the main issue is that formit &ajaxuploadTarget does not accept $variables in it, just plain text. Thats why when i add “/path/to/images/” it works.

That doesn’t matter because FormIt, and thus the ajax upload hook, isn’t executed until after your snippet has completed and returned the generated snippet call. The hook wont see $SingleField, but the actual value of that is inserted into the snippet call before the snippet runs.

Can you please try the echo/exit thing I mentioned to see what the exact snippet call is before it gets processed?

I have removed the echo/print/exit and added return. It works in any case with hardcoded text. If i try to pass variables in it. It will not upload. Im stacked and i have a deadline today. I hate modx

Jees, come on. I’m trying to help you here but you have to at least try and give me what I ask for to help you sort this out. If you’re so pressed for time, then don’t waste both of our time by not providing the information that is requested of you. I can’t read your mind or see your output, so in order to help you out, you need to answer.

What is the output when you use the echo/exit?

Specifically, what I want to know is if with echo/exit you see the literal string “$SingleField” or a value in its place.

If with echo/exit you see the test $SingleField, then your snippet is not setting a proper value to the $SingleField variable, which means your logic for setting the $SingleField value is flawed somehow.

Or if changing it to echo/exit does not change the output, then perhaps this section of the code is triggering:

if (!is_object($modx->query($sql))) {
  return 'No result!';
}

Figuring out the output from your PHP code before it is passed to MODX for parsing will help to give you clarity about where to debug the problem further to get it solved.

1 Like

The echo/exit adds $SingleField in its place correctly. The issue is that the snippet will not return the contents of $SingleField and display them into the formit ajaxuploadTarget

[[!FormIt?
&hooks=AjaxUpload2Formit, HandleFormForUpdate
&preHooks=Formit2AjaxUpload
&ajaxuploadFieldname=image
&ajaxuploadTarget=$SingleField
&ajaxuploadUid=image
&ajaxuploadClearQueue=1
]]

No, that is not added correctly.

By the time echo/exit shows you the snippet call, $SingleField must have already been replaced for it work. Evidently, that’s not happening. That means that it is not getting a value set in your snippet.

To confirm that, add $SingleField = 'some/path/'; before the echo - the expected behaviour is to see some/path in the snippet call that is shown on the page after that.

[[!FormIt?
&hooks= AjaxUpload2Formit, HandleFormForUpdate
&preHooks= Formit2AjaxUpload
&ajaxuploadFieldname= image
&ajaxuploadTarget= images/cyartimages/user-3/2939339
&ajaxuploadUid= image
&ajaxuploadClearQueue= 1
]]

Its correct . The issue is after the return.

1 Like

I think, you should post all your code. Also the part where you call your custom-snippet including the form. I’m sure, there is a little thinking error within your whole setup. Maybe, if one, who knows how MODX and its parsing works and could have a look to the whole picture, this could be fixed very quickly.

1 Like

Its so important to show more of the code, we just can’t know what the unseen pieces are doing, and they can affect things a lot