user7229746
user7229746

Reputation:

How to create unique id for each form submission in contact form 7?

I need to provide a unique id for all participants of the event who is registering for the events online. I wanted to give a unique id something like "17CONF001, 17CONF002, ..."

How to achieve this?

I am using the contact-form-7-dynamic-text-extension plugin for a registration process in my website for this purpose.

Work done:

I am using the following function to generate ticket number.

/* Generate Quote Ticket */
function genTicketString() {
$length = 3;
$iclaa = "17ICLAA";
$characters = "0123456789";
for ($p = 0; $p < $length; $p++) {
    $string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $iclaa.$string;
}
add_shortcode('quoteticket', 'genTicketString');

in my functions.php and I have added

[dynamictext ticket "quoteticket"]

in my form in contact form 7 and made this field invisible through css.

Lastly, I have added [ticket] to your my e-mail body.

As per the solution given by AMCD.

Problems with this code:

Each refresh gives different reference number whereas it should generate the reference number for each submission of forms.

Upvotes: 0

Views: 8416

Answers (2)

Santanu
Santanu

Reputation: 151

If you want to generate random number as an ID just use this.

define( 'CF7_COUNTER', 'cf7-counter' );

function cf7dtx_counter(){
    $length = 8;
    $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters)-1)];
    }
    return $string;
}
add_shortcode('CF7_counter', 'cf7dtx_counter');

function cf7dtx_increment_mail_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1;
    update_option(CF7_COUNTER, $val);
}
add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');

Then, add this to your contact form 7

[dynamichidden Request-ID "CF7_counter"]

Upvotes: 0

fidibsp
fidibsp

Reputation: 39

hope this can help, add this code to your function.php

//Define the key to store in the database
define( 'CF7_COUNTER', 'cf7-counter' );

//Create the shortcode which will set the value for the DTX field
function cf7dtx_counter(){
    $kodeawal = "FJY";
    $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
    return $kodeawal.$val;
}
add_shortcode('CF7_counter', 'cf7dtx_counter');

//Action performed when the mail is actually sent by CF7
function cf7dtx_increment_mail_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
    update_option(CF7_COUNTER, $val); //Update the settings with the new count
}
add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');

Then, add this to your contact form 7

<p>[dynamictext cf7-counter "CF7_counter"]</p>

The output on your form will be "FJY1", "FJY2", and so

Thank you to sevenspark: http://sevenspark.com/tutorials/how-to-create-a-counter-for-contact-form-7

Upvotes: 3

Related Questions