1

First of all, I know this question already exists and already has answers, but I would like to stress in which way my question is different. I have code like this:

$ognjen=array();
    foreach($_POST as $key=>$value){
        if($key!='prenos'&& $key!='submit'){
            if (strrpos($key, '1', -1)){
                if(!empty($_POST[$key])){
                    $uslov=true;//kontrolna promjenljiva
                }else{
                    $uslov=false;
                }
                if($uslov==true){
                    $ognjen[]=$value;
                }else{
                    $_SESSION['message']='You must fill out all fields';
                    unset($_SESSION['message']);
                }
            }//ovdje ide elseif
        }
    }

This code doesn't do what's expected.What I want to achieve is if all values of $_POST are set to put them in $ognjen array,if even one misses, none of them should become part of $ognjen array, but all that inside this foreach loop, because there are some other checkings that need to fulfill. And while searching for answers I couldn't find any that fits to my situation. Please help, I feel that this is pretty simple task to do, but I don't know way to do it.

1 Answer 1

1
<?php
function is_blank($data)
{
    $error_message="";
    foreach ($data as $key => $value)
    {
        if($value=="" or $value==null)
        {
            $error_message.=$key.",";
        }
    }
    if($error_message!="")
    {
        $error_message=substr($error_message, 0,-1);
        return $error_message." is cannot be null or empty.";
    }
    else
    {
        return "";
    }
}

echo is_blank($_POST);
Sign up to request clarification or add additional context in comments.

4 Comments

This is a good answer but I cannot use it the way I would like. If I do this:$ognjen=array(); function is_blank($data) { $error_message=""; foreach ($data as $key => $value) { if (strrpos($key, '1', -1)) { if ($value == "" or $value == null) { $error_message .= $key . ","; }else{ $ognjen[]=$value; } } if($error_message!="") { $error_message=substr($error_message, 0,-1); return $error_message." is cannot be null or empty."; } else { return ""; } } } $prazno=is_blank($_POST); print_r($ognjen); It doesnt print values of $_POST
I cannot get you. you want to check $_POST?
Yes, $_POST is what I need to check.
I modified it a bit, and now it works perfectly. Thanks kind man :)

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.