0

I'm newbie of PHP. I have this input for add pultiple array into MySQL database.

<input name="image_url[]" id="ads" type="text" value="">

now, I need to check if not empty value send to database.

I check with empty() function but this not worked with name="image_url[]".

HTML :

<input name="image_url[]" id="upload" type="text" value="">

PHP code:

if(empty($_POST['image_url'])){

echo 'true';

} else {

echo 'false';

}

But always I see output : true. in default my input is blank/empty.

I check and remove [] from input name(name="image_url") and I see true output.

I think my problem is input array name.

how do fix this and check for empty value?!

in var_dump($_POST) I see this output:

  ["image_url"]=>
  array(1) {
    [0]=>
    string(0) ""
  }

Live DEMO : http://madenade.besaba.com/php/?action=editnews&id=10

NOTE: check in demo with empty value and not empty value! u see output : false

2
  • 1
    try isset($_POST['image_url']) to see is the value is set and is not null Commented Sep 26, 2014 at 6:39
  • Read manuals about what is considered empty Commented Sep 26, 2014 at 6:40

2 Answers 2

1

You're creating an array of values by using [] in the input name. If you only need one value, not more of them, then remove the [] in the input name, otherwise you'll get an array in $_POST['image_url'].

<input name="image_url" id="upload" type="text" value="">
if (empty($_POST['image_url'])) {
    echo 'true';
} else {
    echo 'false';
}

Otherwise, if you do need an array of values, then keeping [] in the input name means that $_POST['image_url'] will contain an array of values, not a single value. You probably want to check each one of them.

<input name="image_url[]" id="upload" type="text" value="">
foreach ($_POST['image_url'] as $image_url) {
    if (empty($image_url)) {
        echo 'true';
    } else {
        echo 'false';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked ture when we have one input box and this code check if one input box have value send and insert all input box (empty value + not empty value) to database. I need to insert only not empty value to my database. <input name="image_url[]" id="upload" type="text" value="123"><input name="image_url[]" id="upload" type="text" value="456"><input name="image_url[]" id="upload" type="text" value=""> I need to send only value 123 and 456 to databse.
@EricFavoni, alright, you can then only keep the values you need in the if branch, and remove what you don't need in the else branch.
0

First use Html Like This(If ur entering single Value)

<input name="image_url" id="upload" type="text" value="">

Then In PHP Code Check Like This

    if(isset($_POST['image_url']) && $_POST['image_url'] != ""){

echo 'true';

} else {

echo 'false';

}

this will give you required output

1 Comment

Why != "" instead of empty()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.