1

I have a list of checkboxes and save the checked values in an array. However, when someone clicks 'submit' and they get an error, all their checked boxes are forgotten. Usually I would let the script remember the checked boxes with a code like

IF checkbox value = OK { echo checked="checked"}

However, now I save it in an array and I have no idea how to do this?

<?php

$sql = "SELECT merknaam FROM merken";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    echo "&nbsp;&nbsp;<input type=\"checkbox\" name=\"merken[]\" value='" . $row['merknaam'] . "'>&nbsp;" . $row['merknaam'] . " <Br />  ";
}

?>

This is the code I use for the checkboxes. Next I display the array with this code:

$merkenstring = implode(",", $_POST['merken']);

echo $merkenstring;

Result: AC Ryan,Adidas,Agu,Cargo

I hope someone could give me a code example!

2 Answers 2

1

Assuming you are posting this to the same page, and $_POST['merken'] is still available after an error, use in_array() to test each checkbox's value against the current set in $_POST:

while ($row = mysql_fetch_array($result)) {
    // If the current value is in the $_POST['merken'] array
    // and the array has been initialized...
    if (isset($_POST['merken']) && is_array($_POST['merken']) && in_array($row['merknaam'], $_POST['merken'])) {
      // Set the $checked string
      $checked = "checked='checked'";
    }
    // Otherwise $checked is an empty string
    else $checked = "";
    // And incorporate it into your <input> tag
    echo "&nbsp;&nbsp;<input $checked type=\"checkbox\" name=\"merken[]\" value='" . $row['merknaam'] . "'>&nbsp;" . $row['merknaam'] . " <Br />  ";
    //----------------------^^^^^^^^^^
}

If this was posted to a different script, you would (as with any post value returned to a previous script) need to store the array in $_SESSION instead and compare against $_SESSION['merken'] in your in_array() call.

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

4 Comments

You Sir are my saviour! Working like a charm after incorporating the variable inside the echo in the right way. Little problem though, at first, when there is no string just yet, I get this error message before very result from the table: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /vhosts/thecompanytree.nl/httpdocs/signup.php on line 487 How do I fix this?
@user1555076 See change above. Test with isset(), is_array() to be sure $_POST['merken'] exists.
Not sure I completely understand, but I'm gonnna work on that. It works like a charm!
@user1555076 Important to understand... The first time your page loads, $_POST is empty, and by extension $_POST['merken'] is not set. So we test if that post key is already set, and if it is set, that it is also an array. Then we know it is safe to use in in_array(). If it was not yet set, then it just proceeds to the else case and makes $checked an empty string.
0

Assuming $row['merknaam'] is the checkbox value, and $_POST['merken'] holds an array of checked checkbox values, then you simply need to check if the value is in the array:

if (in_array($row['merknaam'], $_POST['merken'])) {
   // this checkbox should be checked
}

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.