1

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
  • 1
    mysql_real_escape_string is something else you need to read into, and change name="assign[]" to name="assign" the browser automatically makes an array to send. Commented Sep 10, 2010 at 15:35
  • mmh... the [] need to stay or $_POST['assign'] won't be an array but just contain the value of the last checkbox. Commented Sep 10, 2010 at 15:43

5 Answers 5

3

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.

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

Comments

2

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

Comments

0

are you sure that your FORM is using POST method ?

Comments

0

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.

Comments

-1

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

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.