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`
]]";