Jump to content

Capitalize and remove spaces after form submission


Stefan83

Recommended Posts

Hi - I'm using Wordpress gravity forms plugin. I'd like to force capitalise and remove the spaces within a specific field. This is what I have but its not working.  $_POST[$each] = strtoupper(rgpost($each)) works for making the characters capitals but adding && str_replace(' ', '', $each) after doens't work. What am I doing wrong? Thanks



add_action('gform_pre_submission_22', 'capitalize_fields');
function capitalize_fields($form){
      
        $fields_to_cap = array(
                     'input_64'
										);
        foreach ($fields_to_cap as $each) {
              
                $_POST[$each] = strtoupper(rgpost($each)) && str_replace(' ', '', $each);
				        }
        
        return $form;
} ?>
Link to comment
Share on other sites

'&&' is a logical operator - it doesn't work the way you're trying to use it. Check http://us3.php.net/manual/en/language.operators.logical.php for more details. In the meantime, try

foreach($fields_to_cap as $field){
    $cap_fields[] = strtoupper(str_replace(' ','',rgpost($each)));
}

I'm assuming rgpost() is a custom function that should be called on the raw data?

Link to comment
Share on other sites

For the uppercase, there is also array_change_key_case. Of course, if you need to also remove the spaces, you'll have to use another function.

 

There is also array_map

 

So you could code something like this:

function cap_field($field_to_cap) {
  $rgpost_field = rgpost($field_to_cap); // no idea what rgpost does
  $space_removed = trim($rgpost_field); // use trim() if you want to remove only the spaces at the beginning and end of the field.
  $uppercase = strtoupper($space_removed);
  return $uppercase;
}

$cap_fields = array_map("cap_field", $fields_to_cap);
Instead of the 4 lines there, you could use the line maxxd wrote to you: strtoupper(str_replace(' ','',rgpost($each)));
But, it'll be easier to debug for you. This way, you can var_dump one line after the other. If it works, change it with maxxd's line.
Link to comment
Share on other sites

I was going to post almost the exact same thing as mogosselin, but he beat me to it. Using array_map() with a separate function allows you to decouple the manipulation of the input from the processing of the input. Plus, this is a personal preference, I would put each step of the string manipulation on a separate line instead of one line. Makes it easier to "see" what is going on when you have to debug issues later.

 
Then again, since rpost is a custom function, you could potentially update that to do the additional work and call it using array_map().
 
FYI: The fact that no one has a clue what rgpost() does is a good indication that it is not well named. Don't be afraid of a long name if it helps to identify it's purpose and/or what it does.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.