Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How do I set a computed value in a form in a view that I can read after form submission?

peregrineperegrine MVP
edited September 2012 in Vanilla 2.0 - 2.8

Either I have the concept wrong, the function wrong or something else.

I've got a view with form input

some of wording of the form input is based on a random value, and I need to know the random value that was set after the form is submitted.

I want to set the random value in the form

I tried

$myrand = rand(1,9);

with both of these below: (not sure if this the correct way or which one is correct).

echo $this->Form->Hidden('MyResult', $myrand);

echo $this->Form->AddHidden('MyResult',$myrand);

then I want to read the result in an function in the plugin

e.g.
$Sender->EventArguments['User']['MyResult'];

the dump of " $Sender->EventArguments['User']['MyResult'] " is always string[0] ""

I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Comments

  • peregrineperegrine MVP
    edited September 2012

    don't know if it is the best way to do it - but this how I solved it.

    echo $this->Form->Hidden('MyResult',array('value' => "$myrand"));

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • ToddTodd Chief Product Officer Vanilla Staff

    You usually want to call AddHidden() in your plugin, not the view. This queues up a hidden value which will be rendered by Gdn_Form->Open().

    Your call to Gdn_Form->Hidden(...) is perfectly fine though.

    Bonus advice: mt_rand() is supposedly a better random number generator than rand(). The php docs even go so far as to call rand() dubious (gasp).

  • thanks for the explanation. Todd.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Sign In or Register to comment.