• Resolved jsa922

    (@jsa922)


    Michael,

    I’d been using your suggestions (http://cfdbplugin.com/?page_id=747) for modifying CF7 data before it is saved to the DB. It worked fine at first, but now it’s not working. I don’t know if it is a problem with my PHP code or, perhaps, something to do with the updates in WP and CF7 since I wrote the code in early January.

    I have a form with two checkboxes that are optional. One is to flag a charge entry as a new client, the other is to attach a special services modifier. I have the code in your “Add Actions and Filters” plugin. Unfortunately, even when the checkboxes are left empty, the routine appends the flag/modifier to the respective columns (I do this to shrink the number of columns in a query display via shortcode, otherwise it overflows the page). Here’s the routine, perhaps you can see what I’m missing:

    function chgFilter($formData) {
        $formName = 'Single Charge Submission';
        $chgMod = '';
        $flagNew = '';
        if($formData && $formName == $formData->title) {
        	//Consolidate the charges fields
      	if(empty($formData->posted_data['Mod'])) {
       		$chgMod = '';
       	} else {
       		$chgMod = '–25';
       	}
        	$formData->posted_data['Chg1'] = $formData->posted_data['Chg1'] . $chgMod . "\n\r" . $formData->posted_data['ProlSvc'];
        	//
        	//Flag new clients as part of ID number rather than in a separate column
        	if(empty($formData->posted_data['NewCl'])) {
       		$flagNew = '';
       	} else {
       		$flagNew = 'NEW—';
       	}
        	$formData->posted_data['ClNbr'] = $flagNew . $formData->posted_data['ClNbr'];
        }
        return $formData; // Return the data
    }
    add_filter('cfdb_form_data', 'chgFilter');

    I’ve tried using the isset(foo) function and have also tried matching the POSTed data to the checkbox text “values” in the if statements, but without success. I have also tried changing the form’s checkbox text “values” and trying to append that POSTed data to the other items (that results in “array” as a word being appended!). So, I’m lost on this. Any help would be greatly appreciated!

    https://ww.wp.xz.cn/plugins/contact-form-7-to-database-extension/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    I think “empty()” is not working the way you expect. Also, the code can be reduced a bit. Try this:

    function chgFilter($formData) {
        $formName = 'Single Charge Submission';
        if ($formData && $formName == $formData->title) {
    
            //Consolidate the charges fields
            $chgMod = $formData->posted_data['Mod'] ? '–25' : '';
            $formData->posted_data['Chg1'] = $formData->posted_data['Chg1'] . $chgMod . "\n\r" . $formData->posted_data['ProlSvc'];
    
            //Flag new clients as part of ID number rather than in a separate column
            $flagNew = $formData->posted_data['NewCl'] ? 'NEW—' : '';
            $formData->posted_data['ClNbr'] = $flagNew . $formData->posted_data['ClNbr'];
        }
        return $formData; // Return the data
    }
    
    add_filter('cfdb_form_data', 'chgFilter');

    Thread Starter jsa922

    (@jsa922)

    The code is certainly a lot tighter. However, regardless of whether I click the “New Client” or “Modifier” boxes or leave them unchecked, the revised routine appends “–25” to every charge code and prefixes “NEW–” before every Client ID. Is it possible that there is something being passed with the posted data that makes it look like these boxes have been checked. When I go to the “formal” full view of the whole DB on the CFDB admin page, those columns are blank when they should be. Very odd.

    Plugin Author Michael Simpson

    (@msimpson)

    Let’s see what the values actually are. Add the // debug lines below. Change $logfile to be a valid path. I usually point to the top of my wordpress dir so that I can point to the file in the browser.

    function chgFilter($formData) {
        $formName = 'Single Charge Submission';
        if ($formData && $formName == $formData->title) {
    
            $logFile = '/path/to/log.txt'; // debug
            error_log('Form Submission: ' .print_r($formData, true), 3, $logFile); // debug
    
            //Consolidate the charges fields
            $chgMod = $formData->posted_data['Mod'] ? '–25' : '';
            $formData->posted_data['Chg1'] = $formData->posted_data['Chg1'] . $chgMod . "\n\r" . $formData->posted_data['ProlSvc'];
    
            //Flag new clients as part of ID number rather than in a separate column
            $flagNew = $formData->posted_data['NewCl'] ? 'NEW—' : '';
            $formData->posted_data['ClNbr'] = $flagNew . $formData->posted_data['ClNbr'];
        }
        return $formData; // Return the data
    }
    
    add_filter('cfdb_form_data', 'chgFilter');
    Thread Starter jsa922

    (@jsa922)

    Michael,

    Here’s what the suggested log file shows as values from the checkboxes. From the New Client checkbox the entry is:

    [NewCl] => Array
                    (
                        [0] => New Patient
                    )

    From the Modifier checkbox the entry is:

    [Mod] => Array
                    (
                        [0] => –25
                    )

    When either box is not checked the entry is
    (either [NewCl] or [Mod]):

    => Array
                    (
                        [0] =>
                    )

    All of the other fields, including dropdown boxes, give “straight” data without the “Array” reference. I hope this helps. My PHP knowledge is too limited to figure any more than that it appears the reference to the variable needs to be other than I have done.

    Plugin Author Michael Simpson

    (@msimpson)

    Try:

    function getFirstArrayValue($array) {
        $value = '';
        if (is_array($array) && isset($array[0])) {
            $value = $array[0];
        }
        return $value;
    }
    
    function chgFilter($formData) {
        $formName = 'Single Charge Submission';
        if ($formData && $formName == $formData->title) {
    
            //Consolidate the charges fields
            $chgMod = getFirstArrayValue($formData->posted_data['Mod']) ? '–25' : '';
            $formData->posted_data['Chg1'] = $formData->posted_data['Chg1'] . $chgMod . "\n\r" . $formData->posted_data['ProlSvc'];
    
            //Flag new clients as part of ID number rather than in a separate column
            $flagNew = getFirstArrayValue($formData->posted_data['NewCl']) ? 'NEW—' : '';
            $formData->posted_data['ClNbr'] = $flagNew . $formData->posted_data['ClNbr'];
        }
        return $formData; // Return the data
    }
    
    add_filter('cfdb_form_data', 'chgFilter');

    Thread Starter jsa922

    (@jsa922)

    Michael,

    That works. There may be another solution that I stumbled on.

    After posting the log file data for you I started looking at my PHP references and doing some online searching. For reasons unclear to me, the “value” of a checkbox somehow is being ultimately treated, relative to the variable it’s supposed to be in, as a single entry in a multidimensional array. I tried the following code, therefore:

    $chgMod = $formData->posted_data['Mod'][0] ? '–25' : '';
    //And...
    $flagNew = $formData->posted_data['NewCl'][0] ? 'NEW—' : '';

    This also corrected the problem. From a “proper” programming viewpoint, I’m not sure which is best, but the problem now appears to be fixed, one way or the other. Thanks for helping me get there!!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Changing form data before save not working’ is closed to new replies.