Yuriy T.
Yuriy T.

Reputation: 195

PHP Check if POST method contains one of values

can you please help me with a such issue: I need to check, if in array, which is passed in $_POST contains some specific value, and if yes - does some stuff.. currently I have:

$count=count($_POST['prcategory']);
if(implode($_POST['prcategory']) == "'Cost Collection'" && $count=1 ){
  ...do stuff
}
elseif (implode($_POST['prcategory']) == "'Cost Collection'" && $count > 1 )
{
  ...do other stuff
}
else {.... }

But for the unknown for me reason, when the count is >1, it always goes to the last else option - most probably, it's because I'm not aware, how to properly check, if one of POST values contains my needed value ..

Any suggestions on this?

p.s. all values are passed in quotes, e.g. it's fine, that I have quotes in : 'Cost Collection'

Upvotes: 1

Views: 947

Answers (2)

Pradeep
Pradeep

Reputation: 9707

use simple if else

NOTE : $_POST['prcategory'] should be an array() other wise in_array will not work

    if(isset($_POST['prcategory'])) {
     $count = count($_POST['prcategory']);
     $data = $_POST[prcategory];
     if (in_array("'Cost Collection'", $data)) { // 'Cost Collection' is the value to check in array $data u can use any value
            if ($count == 0 ) {
                //do stuff.......

            }elseif($count > 1)  {
                  //do stuff.......

            }else {
                //do stuff.......
            }

     }else {
            //do stuff.......
          }
   }

Upvotes: 2

Syafiq Azwan
Syafiq Azwan

Reputation: 26

try

if(isset($_POST['prcategory']))
{
   // do stuff...
}

Upvotes: 1

Related Questions