PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Fatal error: Can't use function return value in write context

  1. Hello GF Support!

    Wonderful product! Thank you! I've searched the forums, and have not found an answer to my issue. On my contact form are two radio buttons. Depending on what a user selects in those two radio buttons, the email would be routed to a different email address with a custom subject line. I cobbled together some code based on reading other GF documentation and forum responses, and using the editor in the WP admin area, I pasted this code in my functions.php file, and when I updated the file I got the error message in the subject line. Here is my code:

    add_filter('gform_pre_submission_filter_1', 'modify_sendto_subject');
    function modify_sendto_subject($form){
    
    	// my form uses input_1 for the select box.  Your form may be different.  Change it here as necessary
    	$input_field_1 = 'input_1_1';
    	$input_field_5 = 'input_1_5';
    
    	// clear out any default Send To email addresses or subject lines from the form builder
    	$to = '';
    	$subject = '';
    
    	// Check the form submission for the radio buttons. If there is a value in $input_field_1, then modify the Subject line and the Send To email address
    	if (isset(rgpost($input_field_1)))
    	{
    		// rgpost is a safe way of accessing the $_POST values
    		if (rgpost($input_field_1) == "Loans")
    		{
    			$to = 'emailname1@email.com';
    			$subject = 'Online Contact Form - Loans';
    		}
    		if (rgpost($input_field_1) == "General Banking")
    		{
    			$to = 'emailname2@email.com';
    			$subject = 'Online Contact Form - General Banking';
    		}
    		if (rgpost($input_field_1) == "Web")
    		{
    			$to = 'emailname3@email.com';
    			$subject = 'Online Contact Form - Web';
    		}
    	}
    
    // If no value was selected for the first radio button, then modify the Subject line on whether or not a location preference was or was not selected
    
    	if (!isset(rgpost($input_field_1)))
    	{
    		if (!empty(rgpost($input_field_5)))
    		{
    			$to = 'emailname4@email.com';
    			$subject = 'Online Contact Form - No Subject Selected - Look at Location Preference';
    		}
    		if (rgpost($input_field_5) == "" || rgpost($input_field_5) == NULL)
    		{
    			$to = 'emailname5@email.com';
    			$subject = 'Online Contact Form - No Subject Selected - No Question or Location Preference';
    		}
    	}
    
        // return the modified form object
        return $form;
    }

    Is my code bad? Is there a form setting I need to enable (or disable)? If necessary, here is a link to the form: http://dev.coolwatercreative.com/isb/contact-us/phone-numbers-email/

    Thank you!

    Posted 13 years ago on Wednesday November 30, 2011 | Permalink
  2. Hi purplepatriot,

    Two things I noticed:

    1. the PHP empty() function can not evaluate the return value of a function. In the case where you are using it, you probably don't need it so I'd recommend dropping it.
    2. I see that you are returning the $subject object correctly; however, it doesn't appear that you ever set the $to and $form variables to their respective properties in the form object. If you're updating the Admin notification you can do so like this:
      [php]
      $form['notification']['to'] = $to;
      $form['notification']['message'] = $subject;
    Posted 13 years ago on Wednesday November 30, 2011 | Permalink
  3. Thank you for your very quick reply, David! I made two changes, based on your suggestions. I modified the logic and removed the empty() function, and added the two $form objects. However, I still got the same Fatal Error message, and am stumped. Did I make incorrect changes?

    if (!isset(rgpost($input_field_1)))
    	{
    		if (rgpost($input_field_5) == "Schofield" || rgpost($input_field_5) == "Wausau" || rgpost($input_field_5) == "Weston" || rgpost($input_field_5) == "Any")
    		{
    			$to = 'emailname4@email.com';
    			$subject = 'Online Contact Form - No Subject Selected - Look at Location Preference';
    		}
    		if (rgpost($input_field_5) == "" || rgpost($input_field_5) == NULL)
    		{
    			$to = 'emailname5@email.com';
    			$subject = 'Online Contact Form - No Subject Selected - No Question or Location Preference';
    		}
    	}
    
    $form['notification']['to'] = $to;
    $form['notification']['message'] = $subject;
        // return the modified form object
        return $form;
    Posted 13 years ago on Wednesday November 30, 2011 | Permalink
  4. Can you post your whole functions.php to pastebin.com or pastie.org, and then post the full error message, with line number? It should help pinpoint the cause of the error. Or, with the line number, post the line of code creating the error. That's not always foolproof, because the error could have occurred earlier, but it should work in this case. Thank you.

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  5. I found this at stackexchange as well:
    http://stackoverflow.com/questions/1532693/weird-php-error-cant-use-function-return-value-in-write-context

    Can't use "empty" on a function return as you're doing in the first example. However, the line number will make it more clear.

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  6. David, I got it to work finally. Two things:

    [1] There was also an issue with using isset(). I remove and re-wrote some of the logic.
    [2] I was incorrectly referencing the two radio button form fields: 'input_1_1' should have been 'input_1' and 'input_1_5' should have been 'input_5'.

    Sweet success! Thanks again for getting me started down the right direction!

    Posted 13 years ago on Thursday December 1, 2011 | Permalink
  7. Glad you got that working. Thanks for the update. I'll be sure to let David know.

    Posted 13 years ago on Thursday December 1, 2011 | Permalink

This topic has been resolved and has been closed to new replies.