How to generate a random string to be saved in DB

joezart

New Member
In the fabrik 2.0 forum I found a thread about string random code generator

http://fabrikar.com/forums/showthread.php?t=12269&highlight=random+string

I need to generate a random code to be saved in database
After submitting the form that would be used by users, I need:

To check if the code is not repeated
options:
(yes) run the php code again
(no) save info into database

This would be a hidden element and I also need to sent an email to the user with it's respective "string random code"

Question:
Is the PHP code enough to achived what I need?
How can I sent the email to the user?

PHP:
$length = 7;
  $key = "";
  // define possible characters
  $possible = "0123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRTVWXYZ"; 
  $i = 0;
  while ($i < $length) { 
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
      $key .= $char;
      $i++;
  }
  return $key;
 
hello,

i think you can validate the element using isunique validation. If unique, submit form, else return to form.

for sending mail, use the email plugin (see form/email plugin - you can create a hrml file containing your code) and send the info to desired address. The string can be added to email using placeholders like this {tablename___hidden_element}

Or you can use php plugin (form/php plugin), create a query to retrieve data and add after your code option to send email, something like this:

$mail = JFactory::getMailer();
$recipient[] = 'your_recipient@mail.com';
$mail->sendMail("sender@mail.com", "Sender name", $recipient, "Email title", $msg, true, $cc=null, $bcc=null);
 
hello,

i think you can validate the element using isunique validation. If unique, submit form, else return to form.

for sending mail, use the email plugin (see form/email plugin - you can create a hrml file containing your code) and send the info to desired address. The string can be added to email using placeholders like this {tablename___hidden_element}

Or you can use php plugin (form/php plugin), create a query to retrieve data and add after your code option to send email, something like this:

$mail = JFactory::getMailer();
$recipient[] = 'your_recipient@mail.com';
$mail->sendMail("sender@mail.com", "Sender name", $recipient, "Email title", $msg, true, $cc=null, $bcc=null);


Thanks for your replay
After choosing the "isuniquevalue" validation element's action I understand that I have to place into the condition box the following code:

If unique submit form
else return to form


I'm not a programmer and I'm very ashame to ask this but:


QUESTIONS:

  1. Do I have to use the above (red) code exactly as I wrote it?
  2. Or do I need to write it differently?
  3. If the user is re-turned to the form it will keep the wirtten values or will place the user into a blank new form?

Thanks
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top