Unique number with strtotime
Hi, I would like to create a unique number for each resource I create via a Formit call.
I think the best way is to use a snippet that retrieves current time (with seconds, which is a unique number), that will run as a prehook when the form is loading. Also make a placeholder to use this number in several places inside form.
I am not sure if the code below will give results.
I think the best way is to use a snippet that retrieves current time (with seconds, which is a unique number), that will run as a prehook when the form is loading. Also make a placeholder to use this number in several places inside form.
I am not sure if the code below will give results.
<?php
$unique = date('ymdHis',strtotime('NOW'));
$modx->setPlaceholder('time', $unique);
return '';
Комментарии: 11
It is better to use special function uniqid().
Thank you for the suggestion. Strtotime is enought for my case, although uniqid is more suitable. The problem is that for some reason my Form is not getting proccessed. After I press submit button the form reloads without submitting. If I remove unique from prehooks, it works.
[[!FormIt?
&hooks=`AjaxUpload2Formit, formit2resource, redirect`
&preHooks=`unique,resource2formit,Formit2AjaxUpload`
&resource2formitfields=`template,resource_id,parent,Paratiriseis,pagetitle,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,refnum,IMAGE`
&redirectTo=`106`
&ajaxuploadFieldname=`IMAGE`
&ajaxuploadTarget=`images/uploads/`
&ajaxuploadUid=`image_[[+serianumber]]`
]]
<form class="form" action="[[~[[*id]]]]" method="post" name="dog_input" enctype='multipart/form-data'>
[[!AjaxUpload?
&uid=`image_[[+serianumber]]`
&allowedExtensions=`jpg,jpeg,png,gif`
&maxFilesizeMb=`1`
&maxFiles=`3`
&thumbX=`75`
&thumbY=`75`
]]
Unique snippet
<code><?php
$unique = date('ymdHis',strtotime('NOW'));
$modx->setPlaceholder('serialnumber', $unique);
return '';
1. If you want for I got notification about your comment — please, click to button reply when you write a new comment.
2. Don`t you want to return true from your hook?
2. Don`t you want to return true from your hook?
Remember to return true if your preHook or Hook is successfulrtfm.modx.com/extras/revo/formit/formit.hooks#FormIt.Hooks-UsingpreHooks
Thanx again Vasily. Now the form submits but other problem occured.
[[+serialnumber]] at &ajaxuploadUid=`image_[[+serialnumber]]` returns blank. I guess that snippet inside Formit call doesn't work. In ground zero again…
[[+serialnumber]] at &ajaxuploadUid=`image_[[+serialnumber]]` returns blank. I guess that snippet inside Formit call doesn't work. In ground zero again…
[[!FormIt?
&hooks=`AjaxUpload2Formit, formit2resource, redirect`
&preHooks=`unique,resource2formit,Formit2AjaxUpload`
&resource2formitfields=`template,resource_id,parent,Paratiriseis,pagetitle,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,refnum,IMAGE`
&redirectTo=`106`
&ajaxuploadFieldname=`IMAGE`
&ajaxuploadTarget=`images/uploads/`
&ajaxuploadUid=`image_[[+serialnumber]]`
]]
Yes, of course. Hook is called after form submission.
But you can insert any value into submitted data inside hook — see docs.
But you can insert any value into submitted data inside hook — see docs.
Hook is called after, correct. But preHook is called when Form loads, right?
So unique snippet runs, and placeholder should work. No?
So unique snippet runs, and placeholder should work. No?
Maybe. That you need to call placeholder uncached, at least.
Anyway, you could assign any value to submitted data in hook:
&ajaxuploadUid=`image_[[!+serialnumber]]`
Anyway, you could assign any value to submitted data in hook:
<?php
$hook->setValue('serialnumber', uniqid());
return true;
So my snippet (unique) will be:
I will run it as a prehook. And how I retrieve this value (serialnumber) in &ajaxuploadUid?
P.S. Sorry for my naive questions.
<?php
$hook->setValue('serialnumber', uniqid());
return true;
I will run it as a prehook. And how I retrieve this value (serialnumber) in &ajaxuploadUid?
P.S. Sorry for my naive questions.
You do not need to run preHook to set placeholder and then submit it with form when you could add it directly to submitted data from hook that will run after submission.
Why do you need this random sting in form, by the way?
Why do you need this random sting in form, by the way?
This random number makes every Ajaxupload queue unique and I saved it as a tv so I can retrieve it easily.
What I want, is to give the user the ability to edit own resources. Almost working. Only one problem left.
What I want, is to give the user the ability to edit own resources. Almost working. Only one problem left.
[[!unique]]
[[!FormIt?
&hooks=`AjaxUpload2Formit, formit2resource, redirect`
&preHooks=`resource2formit,Formit2AjaxUpload`
&resource2formitfields=`template,resource_id,parent,Paratiriseis,pagetitle,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,refnum,IMAGE`
&redirectTo=`106`
&ajaxuploadFieldname=`IMAGE`
&ajaxuploadTarget=`images/uploads/`
&ajaxuploadUid=`image_[[+serianumber]]`
]]
<form class="form" action="[[~[[*id]]]]" method="post" name="dog_input" enctype='multipart/form-data'>
[[!AjaxUpload?
&uid=`image_[[+serianumber]]`
&allowedExtensions=`jpg,jpeg,png,gif`
&maxFilesizeMb=`1`
&maxFiles=`3`
&thumbX=`75`
&thumbY=`75`
]]
</form>
Unique snippet
<?php
$unique = date('ymdHis',strtotime('NOW'));
$modx->setPlaceholder('serialnumber', $unique);
return '';
Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.