Jump to content

[SOLVED] Removing empty values from $_POST array


kalevra

Recommended Posts

I have a form submitted to a script.

 

I get the values from the form using:

foreach($_POST as $key=>$value){
      $$key = $value;
      $search[] .= "$$key";
      $replace[] .= "$value";
}

(Variables $search & $replace are for use in a str_replace())

 

Now let's say some fields were left empty in the form I will have some $replace left empty.

If I wanted to remove from both arrays the fields left empty, would this work?

(Sorry I can't try myself no access to PHP server for next 2 hours:()

 

Does it even set a $_POST variable if it's left empty?

 

foreach($_POST as $key=>$value){
         if(empty($value){ 
             unset($_POST[$key]);
             }else{
       $$key = $value;
       $search[] .= "$$key";
       $replace[] .= "$value";
       }
}

You could check the contents while you're looping through the array...

<?php
foreach($_POST as $key=>$value){
    if(!empty($value)) {
        $$key = $value;
        $search[] .= "$$key";
        $replace[] .= "$value";
    }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.