PHP - code to submit a second form when submitting an initial form

By: Richard Posner | Asked: 08/14/2024
ForumsCategory: General questionsPHP - code to submit a second form when submitting an initial form
Richard Posner asked 3 months ago
  • I have two forms:
A registration Form and a Detail Form When the user submits the Registration form, I would like the detail form to be created and submitted with the user ID that is on the Registration to populate the User ID on the Detail form.   I have some code that should work, but it uses non-numerical field keys which is why the code bombs.   Registration Form ID = 2 Registration User_ID Field Key = zah2u Detail Form ID = 43 Registration User_ID Field Key = udy9s33334   Can someone please help me fix this code?  
Start your code here
add_action(\'frm_after_create_entry\', \'create_detail_form_entry\', 20, 2);
function create_detail_form_entry($entry_id, $form_id) {
// Replace 123 with your Registration Form ID
if ($form_id == 2) {
$user_id = FrmEntryMeta::get_entry_meta_by_field($entry_id, zah2u); // Replace \'user_id_field\' with the actual field key for user ID in your Registration Form // Check if an entry already exists in the Detail Form for this user
$existing_entry = FrmEntry::getAll(array(
\'it.form_id\' => 43, // Replace 456 with your Detail Form ID
\'meta_key\' => udy9s33334, // Replace \'user_id_field\' with the actual field key for user ID in your Detail Form
\'meta_value\' => $user_id
)); if (empty($existing_entry)) {
// Prepare the data for the Detail Form
$detail_form_id = 43; // Replace 456 with your Detail Form ID
$new_entry = array(
\'form_id\' => $detail_form_id,
\'item_meta\' => array(
udy9s33334 => $user_id, // Replace \'user_id_field\' with the actual field key for user ID in your Detail Form
// Add other default fields if necessary
),
); // Create the new entry in the Detail Form
FrmEntry::create($new_entry);
}
}
}
6 Answers
Rob LeVineRob LeVine answered 3 months ago
A few things:
  • What does the "code bombs" mean? What line? What error?
  • I suggest putting the string values in quotes. e.g., 'meta_key' => udy9s33334 should be 'meta_key' => 'udy9s33334'
  • I don't see how the $new_entry value will work for FrmEntry::create. If the field key is "udy9s33334" then the item_meta array should be FrmField::get_id_by_key("udy9s33334") => $user_id otherwise, the eventual DB insert command will have no idea what to do with it.
  • It's better to use a code-pasting website to put code in the forms so that it's readable.
Richard Posner answered 3 months ago
Thank you for your response.   I am trying to follow your instructions.  Please let me know if I made the correct changes.   1. The code stops running.  In the old code it was  udy9s33334 => $user_id, 2. Putting ' ' around udy9s33334 doesn't.t break the code, but it doesn't add a user id to the second form 3. I put the ' ' back around both instances of udy9s33334 4. I switched       From:          'item_meta' => array( //old code
                           To:             item_meta' = FrmField::get_id_by_key("udy9s33334") => $user_id 
  Is this what you meant?  
Code Starts Here
Start your code hereadd_action('frm_after_create_entry', 'create_detail_form_entry', 20, 2); function create_detail_form_entry($entry_id, $form_id) { // Replace 123 with your Registration Form ID if ($form_id == 2) { $user_id = FrmEntryMeta::get_entry_meta_by_field($entry_id, zah2u); // Replace 'user_id_field' with the actual field key for user ID in your Registration Form // Check if an entry already exists in the Detail Form for this user $existing_entry = FrmEntry::getAll(array( 'it.form_id' => 43, // Replace 456 with your Detail Form ID 'meta_key' => 'udy9s33334', // Replace 'user_id_field' with the actual field key for user ID in your Detail Form 'meta_value' => $user_id )); if (empty($existing_entry)) { // Prepare the data for the Detail Form $detail_form_id = 43; // Replace 456 with your Detail Form ID $new_entry = array( 'form_id' => $detail_form_id, //'item_meta' => array( //old code item_meta' = FrmField::get_id_by_key("udy9s33334") => $user_id
//udy9s33334 => $user_id = (string), // Replace 'user_id_field' with the actual field key for user ID in your Detail Form 'udy9s33334' => $user_id, // Replace 'user_id_field' with the actual field key for user ID in your Detail Form // Add other default fields if necessary ), ); // Create the new entry in the Detail Form FrmEntry::create($new_entry); } } }
 
Rob LeVineRob LeVine answered 3 months ago
At least the code is well-formatted now :). While "the code stopped running" is not the detailed information I was looking for, I see a lot of problems in the code (no offense intended). The parameters in the FrmEntry::getAll command will never result in it finding an entry because there are no Formidable tables that have meta_key as a column. An easier way to accomplish that is to create your own SQL query that searches frm_item_metas for a match. Additionally, I'm puzzled by what udy9s33334 is. Is it a field key? As for the code that creates a new entry, there are problems there as well. I suggest using this example as a starting point. Finally, the code you pasted doesn't even compile, so it'll throw an error as soon as it's called. You can use a PHP Code Checker to validate syntax.
Richard Posner answered 3 months ago
Rob, I appreciate your efforts, but i now more confused than I was.   I got the inital code from Formidable.  I am going to place teh original code below. Please let me restate the original issue in greater detail. I have two forms, a Registration Form which I complete and a Detail Form which the user completes. The Registration has many fields, but the ones that I am interested in are the First Name, Last Name, User ID. and Employee ID The Detail form has many fields but also includes the First Name, Last Name, User ID. and Employee ID When I complete the Registration form and submit it, I would like the Detail form to be submitted with the First Name, Last Name, User ID. and Employee ID populated. The original code that I got from Formidable was meant to Create and submit the Detail form and populate the User ID field.  I had hoped that once this worked, I could add the First Name, Last Name and Employee ID. This original code does create and submit the Detail form when the Registration form is submitted, but it does not populate the User ID in the Detail form so that the Registration and Detail forms are not linked in any manner. I have made many modifications based upon input from other, and nothing has worked.  I understand the steps are to: Capture the User ID from the Registration form and then create a Detail form, Populate the User ID and submit the detail form.  Please see the original code below, and please if you would like, kindly suggest changes    Registration Form ID = 2 Registration User_ID Field Key = zah2u Detail Form ID = 43 Detail User_ID Field Key = udy9s33334   
Start your code here
add_action('frm_after_create_entry', 'create_detail_form_entry', 20, 2); function create_detail_form_entry($entry_id, $form_id) { // Replace 123 with your Registration Form ID if ($form_id == 2) { $user_id = FrmEntryMeta::get_entry_meta_by_field($entry_id, zah2u); // Replace 'user_id_field' with the actual field key for user ID in your Registration Form // Check if an entry already exists in the Detail Form for this user $existing_entry = FrmEntry::getAll(array( 'it.form_id' => 43, // Replace 456 with your Detail Form ID 'meta_key' => 'udy9s33334', // Replace 'user_id_field' with the actual field key for user ID in your Detail Form 'meta_value' => $user_id )); if (empty($existing_entry)) { // Prepare the data for the Detail Form $detail_form_id = 43; // Replace 456 with your Detail Form ID $new_entry = array( 'form_id' => $detail_form_id, 'item_meta' => array( 'udy9s33334' => $user_id, // Replace 'user_id_field' with the actual field key for user ID in your Detail Form // Add other default fields if necessary ), ); // Create the new entry in the Detail Form FrmEntry::create($new_entry); } } }
             
Rob LeVineRob LeVine answered 3 months ago
Enable WP Debugging and put debug statements in the code to output information to a log file. My final guess on this is that $user_id is null, so the new entry is not being created. And my guess on why $user_id is null is because zah2u needs quotes around it $user_id = FrmEntryMeta::get_entry_meta_by_field($entry_id, zah2u). I've been writing custom code with Formidable for 8 years (and PHP for 20 years) and have never seen several of the things in the above code such as passing strings without quotes around it. You say you got the code from Formidable and yet I'm at a loss for where or how. I just created a quick PHP program calling FrmEntryMeta::get_entry_meta_by_field with its second parameter being an unquoted string and PHP fell over and died with "Uncaught Error: Undefined constant"
Richard Posner answered 3 months ago
Rob,   I put the ' ' back in. It runs without error, and does create the Detail form, but does not populate the User ID on the Detail form The replacement code is not right, but I don't know why, and that is why I posted. I would be appreciative if you could please tell me what is wrong with the code. Thx Richard        
Start your code hereadd_action('frm_after_create_entry', 'create_detail_form_entry', 20, 2);
function create_detail_form_entry($entry_id, $form_id) {
    // Replace 123 with your Registration Form ID
    if ($form_id == 2) {
        $user_id = FrmEntryMeta::get_entry_meta_by_field($entry_id, 'zah2u'); // Replace 'user_id_field' with the actual field key for user ID in your Registration Form

        // Check if an entry already exists in the Detail Form for this user
        $existing_entry = FrmEntry::getAll(array(
            'it.form_id' => 43, // Replace 456 with your Detail Form ID
            'meta_key' => 'udy9s33334', // Replace 'user_id_field' with the actual field key for user ID in your Detail Form
            'meta_value' => $user_id
        ));

        if (empty($existing_entry)) {
            // Prepare the data for the Detail Form
            $detail_form_id = 43; // Replace 456 with your Detail Form ID
            $new_entry = array(
                'form_id' => $detail_form_id,
                'item_meta' => array( 			
							
	   'udy9s33334' => $user_id, // Replace 'user_id_field' with the actual field key for user ID in your Detail Form
                    // Add other default fields if necessary
                ),
            );  

            // Create the new entry in the Detail Form
            FrmEntry::create($new_entry);
        }
    }
}

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right