重力形式:更新条目而不创建新条目

问题描述 投票:0回答:1

我正在使用重力形式插件。我已将电子邮件数据的 CSV 作为表单条目上传。我需要允许查看者使用表单来使用附加数据更新当前条目。 (他们可以更改除电子邮件地址之外的所有内容。)一旦他们点击提交,我希望更新现有条目(通过电子邮件字段检查)。我有这个工作。然而,重力形式也正在创建一个新的空条目。我尝试在设置中关闭条目创建,但现有条目会被空白数据覆盖。

这是我正在运行的代码:

add_action( 'gform_pre_submission_1', 'update_form_by_email', 10, 2 );
function update_form_by_email($form_id) {
    $search_criteria = array(
        'status'    => 'active',
        array(
            'key'   => '6',
            'value' => rgpost('input_6')
        ),
    );
    $entryIDs = GFAPI::get_entry_ids( $form_id, $search_criteria );

    if($entryIDs) {
        foreach ($entryIDs as $entryID) {
            $result = GFAPI::update_entry($entry, $entryID);        
            return $result;
        }
    } else {
        add_filter( 'gform_validation_message_1', 'email_error_message', 10, 2 );
        function email_error_message( $message, $form ) {
            return '<div class="validation_error">This email is not valid. Please try again.</div>';
        }
    }
}

关于如何防止每次创建空条目有什么想法吗?

谢谢!

wordpress gravity-forms-plugin
1个回答
0
投票

您似乎正在尝试根据给定的电子邮件地址更新重力表格中的现有条目,但尽管您努力通过关闭选项中的条目创建来阻止它,但仍会生成一个新的空条目。

add_action( 'gform_pre_submission_1', 'update_form_by_email', 10, 1 );
function update_form_by_email($form) {
    // Get the email input field value from the submitted form
    $email = rgpost('input_6');

    if (!empty($email)) {
        // Define the search criteria to find an existing entry by email
        $search_criteria = array(
            'status' => 'active',
            array(
                'key' => '6', // Adjust the field key as needed
                'value' => $email,
            ),
        );
        $entryIDs = GFAPI::get_entry_ids($form['id'], $search_criteria);

        if ($entryIDs) {
            // Since there might be multiple entries with the same email, consider how you want to update them
            foreach ($entryIDs as $entryID) {
                $entry = GFAPI::get_entry($entryID);
                // Update the entry with the new data from the submitted form
                // You may use the rgar() function to retrieve form field values: rgar($entry, '6');
                // Update fields as needed, excluding the email field
                $entry['6'] = rgpost('input_6'); // Update the email field if needed

                // Save the updated entry
                GFAPI::update_entry($entry);

                // You can add custom success messages here if needed
                // Example: $form['confirmation']['type'] = 'success';
                // Example: $form['confirmation']['message'] = 'Entry updated successfully.';
            }
        } else {
            // The email doesn't match any existing entries
            add_filter( 'gform_validation_message_' . $form['id'], 'email_error_message', 10, 2 );
        }
    }
}

function email_error_message($message, $form) {
    return '<div class="validation_error">This email is not associated with an existing entry. Please try again.</div>';
}
© www.soinside.com 2019 - 2024. All rights reserved.