This forum is no longer open and is for reading/searching only.
Please use our new MachForm Community Forum instead.
MachForm Community Forums » MachForm 2
Random number as a field's default value
Started 13 years ago by bonda | 6 posts |
-
I want to attribute to every entry its unique 4-digit ID and put it into an admin-only field. A random number generated by FLOOR(1001 + RAND() * 8998) would do it. But how to get it generated and entered automatically when the entry is created? And how to make sure it is not regenerated in case of the entry update?
Posted 13 years ago # -
It's pretty easy to do this. You only need to edit "includes/post-functions.php" file.
Search around line 967-968 and you'll find this block of code:$table_data['ip_address'] = $user_ip_address; $table_data['date_created'] = date("Y-m-d H:i:s");
Now, let say your form is having id number 7 and you have a number field (admin only field) which having id = "element_5" (view the HTML source of your form to see it).
Below the above code, add this code:
if($form_id == 7){ $table_data['element_5'] = FLOOR(1001 + RAND() * 8998); }
that should do it.
MachForm Founder
Posted 13 years ago # -
Thank you, Andar!
Posted 13 years ago # -
I know this an very old post, but it does seem to work in 4.6 its now located on line 1969-1970
My question is, although it works, if you edit the entry in the back-end it will change the randomized number again. Is there anyway to stop this from happening with like a - if(empty($var)) statement? I'm not a programmer so forgive my ignorance.
In my thinking, if there were a way for it to check and see if the field was populated it would be not change it. Or in other words - if empty then run $table_data['element_1'] = FLOOR(210 + RAND() * 300); if populated then ignore.
Please help!
Thanks!
AaronPosted 9 years ago # -
You'll only need to adjust the if statement a little bit, like this:
if($form_id == 7 && $is_edit_page !== true){ $table_data['element_5'] = FLOOR(1001 + RAND() * 8998); }
That should prevent the code being executed when you're editing the entry in the backend.
MachForm Founder
Posted 9 years ago # -
Works Great!!! Thanks so much!
Although, when you're filling out the form and you get to the review page, it shows the random number (which is great), but if you decide to click previous to correct something and then continue back to the review page the random number changes. Any we to stop it from changing once it generated?
Thanks for everything!
Aaron
Posted 9 years ago #
Reply
You must log in to post.