0

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";                 
}
4
  • var_dump($folder) before if(!empty($folder)). What you get? Commented Oct 24, 2012 at 16:00
  • 1
    @insertusernamehere I am using PDO, I don't need to sanitize. Commented Oct 24, 2012 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) "" } Commented Oct 24, 2012 at 16:06
  • Ah OK, didn't see that. Deleted my comment. Commented Oct 24, 2012 at 16:06

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This'll work. I'll leave out giving an error. Might confuse people anyway. Thank you.
2

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>

...

Comments

0

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

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

2 Comments

That won't process the boxes that DO have names. My code probably confused you. I'll take out my check.
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.
0

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)

Comments

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.