0

HTML markup

<input name="one[name]">
<input name="one[email]">
<input name="two[message]">
...
alot input
..

I pass that two array data from jquery to php, i need check if the field is empty by php and exit when find one of them is empty.

But i dont want do it one by one, can it done by php function like foreach or other?

This is what i tried but fail.

$data_one = $_POST['one'];
$data_two = $_POST['two'];

if (empty( $_POST['one'] )) { // i only need check `$data_one` in this example
    exit('some field are empty');
} else {
    echo('field are filled');
    // continue other function
}

Above code keep return field are filled message, whether i fill the input field or not.

Thanks so much.

2
  • Var_dump it, what is there? Commented Jan 25, 2016 at 4:16
  • Each of them return string(0). Commented Jan 25, 2016 at 4:43

4 Answers 4

3
$allFilled = true;
foreach($_POST['one'] as $key=>$value){
    if(empty($value)){
        $allFilled = false;
        exit('some fields are empty');
    }
}
if($allFilled){
    exit('all fields are filled');
}
Sign up to request clarification or add additional context in comments.

3 Comments

That what i need, but how can i run a function only one time when all of $_POST['one'] is not empty? i tried foreach($_POST['one'] as $key=>$value){ if(empty($value)){ exit('some field are empty'); } else { echo('all field (one) are filled'); // function, this function run two times when all field filled. } } but the function run two times when both of $_POST['one'] name and email is filled, thanks.
@aboutjquery simple thing is to use functions like array_filter which requires no looping!
i modified the answer
2

Making use of array_filter and count functions

<?php
$data_one = $_POST['one'];
$data_one_filter = array_filter($_POST['one']); //Remove indexes of null or 0 - certainly name and email can't be 0
$data_one_count = count($_POST['one']); //count actual number of POST variables

$data_two = $_POST['two'];

if (count($data_one_filter) === $data_one_count) { 
    exit('fields are filled');
} else {
    echo('some fields are empty');
    // continue other function
}

Comments

0

You are sending data as array. So you seed to check this data as array like this:

$data_one = $_POST['one']['name'];
$data_two = $_POST['two']['message'];

if (empty( $_POST['one']['name'] )) { // i only need check `$data_one` in this example
    exit('some field are empty');
} else {
    echo('field are filled');
    // continue other function
}

Comments

0

Try this

foreach($_POST['one'] as $key=>$value){
    if(empty($value)){
        exit('some field are empty');
    }
}

echo "All fields are filled";

1 Comment

Before posting an answer check other answers. @Hoopriver already posted this answer

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.