1

Here's one that has me stumped.

I am storing the $_POST array in a mysql database. I am using JavaScript to dynamically create input fields on my form too but if an input field is empty is still gets inserted into the database (obviously).

Is there anyway to go through the POST array and filter these empty values out?

EDIT:

Using WordPress but same idea:

<?php 
foreach($_POST['eirepanel_inline_ads_options_name'] as $post_eirepanel_inline_ads_options_name):
    if(empty($post_eirepanel_inline_ads_options_name)): 
       echo 'empty';
    else: 
        update_option('eirepanel_inline_ads_options', $_POST);
        $eirepanel_inline_ads_options = get_option('eirepanel_inline_ads_options');
    endif; 
endforeach;
?>
4
  • How do you deal with submit button values? What do you consider an empty value? Empty string? 0? Commented Jun 15, 2011 at 12:18
  • Submit values have a boolean value. An empty value would be an empty string Commented Jun 15, 2011 at 12:22
  • Hmm, what is the text displayed on the submit-button? Commented Jun 15, 2011 at 12:24
  • Are you making use of arrays in your form (PHP arrays via form-field names)? Commented Jun 15, 2011 at 12:40

6 Answers 6

2

You said that you only want the elements of the $_POST array that are not an empty string:

   $nonEmpty = array_filter($_POST, function($value) {return $value!=='';} );

You wrote you add fields dynamically. I don't know if you make use of the PHP notation for arrays in form fields. If so things do get a little more different to deal with that additional type of data.

Sign up to request clarification or add additional context in comments.

Comments

1

There are several. However, beware of controls that don't always send values. You won't necessarily get a value for a checkbox if it's not ticked.

Since you're using Javascript, how about you store the form as JSON:

<?php
$formVars = array();
foreach ($_POST as $key=>$value){
   if ($value != ''){
      $formVars[$key] = $value;
   }
}

$form = json_encode($formVars);//Store this in DB

Comments

0

Check the value of the $_POST before inserting anything in the database

if($_POST['email'] == "") { ...

Comments

0

You could do something like that :

foreach ($_POST as $key => $value)
{
    if (empty($value))
    {
        unset($_POST[$key]);
    }
}

Comments

0

You could just do an empty check on it.

DISCLAIMER This is a simple example. Does not check whether $value is an array (e.g. checkbox or file input). Works only for inputs.

$data = array();
foreach ($_POST as $key => $value) {
   if (empty($value)) continue;
   $data[$key] = $value;
}
//save $data to DB

Comments

0
function filter_post($value)
{ 
  if ($value !='')
  {
    return $value;
  }

}
$new_array=array_filter($_POST,'filter_post');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.