views:

52

answers:

5

I have this piece of code I've done so far:

$assign = $_POST['assign'];

if(!empty($name) && !empty($description) && !empty($deadline))
{
    if(validateDate($deadline))
    {
        $final_deadline = strtotime($deadline);

        $sql .= "INSERT INTO projects
                (project_id, project_name, project_description, project_deadline, project_status, project_priority)
                VALUES ('" . $project_id . "', '" . $name . "', '" . $description . "', '" . $final_deadline . "', '" . $status . "', '" . $priority . "');";

        if(is_array($assignments))
        {
            foreach($assignments as $assigned_user)
            {
                $sql .= "INSERT INTO assignments (user_id, project_id) VALUES('" . $assigned_user . "', '" . $project_id . "');";
            }
        }
        else
        {
            echo '<br /><br />not an array<br />';
        }

        $result = mysql_query($sql) or die(mysql_error());

        echo '<br />' . var_dump($assignments);
    }
    else
    {
        $_SESSION['ERROR'] = "Not a valid deadline";
        header("Location: dashboard.php");
    }
}

Which would use this list of checkboxes:

<input type="checkbox" value="<?php echo $row['user_id']; ?>" id="<?php echo $row['username']; ?>" class="usercheckbox" name="assign[]" />

The problem that comes is when I submit the form which is to insert a record for each of the checked boxes, it returns an empty array...

It says this:

Notice: Undefined index: assign in [..] on line 41

not an array

NULL

So basically the checkboxes have nothing, the checkboxes already have values in them, the assign[] is correct, now I don't know what the problem is that it doesn't contain anything when submitting the form.

+2  A: 

You're using assign[] in your FORM code and in your PHP code you're using $assignments, these become two different variables.

2. when checking against checkboxes I think there value turns into 'checked' not a value (could be wrong on this been a while since I did it)

Edit I was wrong, it does use the value

Viper_Sb
A: 

are you sure that your FORM is using POST method ?

From.ME.to.YOU
+3  A: 

Are any of the checkboxes checked? If you submit the form with all the boxes unchecked, it won't POST anything.

If you want the warning message to go away add a if(isset($_POST['assign'])) before you set $assign. Then add an else { $assign = array() } This will initialize $assign to an empty array so you won't get more warnings about using $assign in a null state.

jonescb
A: 

Your code is right. Try $_REQUEST[] instead of $_POST. Make sure at least one checkbox is checked before sumitting.

David
His code is not right. `$assign != $assignments`.
ZeissS
A: 

First intention is that I think you didn't used a form tag or the wrong method attribute.

<form method="post" action="target.php">
....
</form>

if that isn't the case try a var_dump on the Post array

var_dump($_POST);

in the Target PHP file, if it is empty there was nothing send. As I remember right a checkbox only gets in the array if it is selected so try using

if(isset($_POST['assign'])){
   //...
}else{
   //What to do if it isn't set.
}

If you that doesn't solve your problem ask again and post you var_dump.

Spectator