Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to display an error if one of the <input> boxes the user has submitted is empty, but I also want to process the boxes that do not have empty names.

Here is the form: http://jsfiddle.net/Draven/rEPXM/27/

PHP:

if(isset($_POST['addFolder']))
{    
    foreach($_POST['folder'] as $id => $value)
    {
        $database->query('INSERT INTO folders (name) VALUES (?)', array($value));
    }
    $success[] = "Folder(s) added";                 
}
share|improve this question
var_dump($folder) before if(!empty($folder)). What you get? – zarkoz Oct 24 '12 at 16:00
1  
@insertusernamehere I am using PDO, I don't need to sanitize. – Draven Oct 24 '12 at 16:01
@zarkoz I added two input boxes, the first one had a name "TEst", the second one was empty. array(2) { [0]=> string(4) "TEst" [1]=> string(0) "" } – Draven Oct 24 '12 at 16:06
Ah OK, didn't see that. Deleted my comment. – insertusernamehere Oct 24 '12 at 16:06

4 Answers

up vote 1 down vote accepted

Try as below

foreach($folder as $id => $value)
{
    if($value){
      $database->query('INSERT INTO folders (name) VALUES (?)', array($value));
    }
    else {
       $error[]=$id;
    }
}

$error contains all indexes which doesn't have value.

share|improve this answer
This'll work. I'll leave out giving an error. Might confuse people anyway. Thank you. – Draven Oct 24 '12 at 16:11

Try this:

$folder = isset($_POST['folder']) && is_array($_POST['folder'])
        ? $_POST['folder']
        : array();
$errors = array();
foreach ($folder as $id => $value) {
    $value = is_array($value) ? '' : trim($value);
    if ($value == '') {
        $errors[$id] = 'Empty value';
        continue;
    }

    $database->query('INSERT INTO folders (name) VALUES (?)', array($value));
}

Edit your template:

...

<div id="foldercontainer">
<?php foreach ($errors as $error): ?>
    <div>
        <div><?php echo $error ?></div>
        <input name="folder[]" type="text" size="20" value="" />
    </div>
<?php endforeach ?>
</div>

...
share|improve this answer

if(!isset($folder) || $folder === "")

But I do not understand whyyou are running the query if you if $folder is empty...

share|improve this answer
That won't process the boxes that DO have names. My code probably confused you. I'll take out my check. – Draven Oct 24 '12 at 16:00
I don't understand. If this condition is true, don't do the query. Or just negate the expression if you want to do it the opposite way. – thatidiotguy Oct 24 '12 at 16:01

Complementing what was said. Use trim() when you want to test if a value is empty. You do not want to name folders with empty values, right?​​:

$folder = array('aaaa', 'bbbb', ' ', '    ');

foreach($folder as $id => $value) {
    $value = trim($value);

    if(!empty($value)) {
      var_dump($value);
    } else {
       $error[]=$id;
    }
}

On the way up the output will be:

string 'aaaa' (length=4)

string 'bbbb' (length=4)

Now, without using trim() and test if it is not empty, the output is(and it is a mistake):

string 'aaaa' (length=4)

string 'bbbb' (length=4)

string ' ' (length=1)

string '    ' (length=4)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.