How to generate UUID and place in hidden form field

How to generate UUID and place in hidden form field? I’d like the UUID to start something like XXX-99999999 or similar.

Create a text field, and populate it on Insert with the function code below :

function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

alternatively use, use uniqid from PHP :

[SIZE=14px]printf[/SIZE][SIZE=14px]([/SIZE][SIZE=14px]"uniqid(‘php_’): %s
"[/SIZE][SIZE=14px], [/SIZE][SIZE=14px]uniqid[/SIZE]SIZE=14px);[/SIZE]

Make sure to index the field as well.

When you say ‘populate it on insert’? Are you talking about the onAfterInsert event? Sorry - I’m new to this.

I would say before Insert (onBeforeInsert) is more appropriate.

If you need the guid in your code you can consider onload event.