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";
       }
}

Link to comment
Share on other sites

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";
    }
}
?>

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.