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.

<?php
$unique = date('ymdHis',strtotime('NOW'));
$modx->setPlaceholder('time', $unique);
return '';
Poko Loko
16 февраля 2016, 08:35
modx.pro
2 995
0

Комментарии: 11

Василий Наумкин
16 февраля 2016, 11:41
0
It is better to use special function uniqid().
    Poko Loko
    16 февраля 2016, 16:44
    0
    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 '';
      Василий Наумкин
      16 февраля 2016, 19:01
      0
      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?
      Remember to return true if your preHook or Hook is successful
      rtfm.modx.com/extras/revo/formit/formit.hooks#FormIt.Hooks-UsingpreHooks
        Poko Loko
        16 февраля 2016, 22:10
        0
        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…

        [[!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]]`
        ]]
          Василий Наумкин
          16 февраля 2016, 22:34
          0
          Yes, of course. Hook is called after form submission.

          But you can insert any value into submitted data inside hook — see docs.
            Poko Loko
            17 февраля 2016, 02:37
            0
            Hook is called after, correct. But preHook is called when Form loads, right?
            So unique snippet runs, and placeholder should work. No?

              Василий Наумкин
              17 февраля 2016, 05:15
              0
              Maybe. That you need to call placeholder uncached, at least.
              &ajaxuploadUid=`image_[[!+serialnumber]]`

              Anyway, you could assign any value to submitted data in hook:
              <?php
              $hook->setValue('serialnumber', uniqid());
              return true;
                Poko Loko
                17 февраля 2016, 12:38
                0
                So my snippet (unique) will be:

                <?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.
                  Василий Наумкин
                  17 февраля 2016, 13:04
                  0
                  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?
                    Poko Loko
                    17 февраля 2016, 17:36
                    0
                    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.
      Илья Уткин
      17 февраля 2016, 16:28
      0
      [[!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 '';
        Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
        11