1

I'm having a couple problems and I think they are all somewhat interconnected.

brief summary of what i'm attempting to do. I'm taking a sql query and i'm looping through the results to build a users table. Then what i'm trying accomplish is for an admin to be able to activate and suspend users by a simple button click. With that being said, let me show the issues i'm having.

1) I'm having issues defining the input type so that each rows checkbox value attibute is the same as that users db id. Then table is constructed inside a while loop using syntax similar to $var .= whatever; var .= whatever2; etc which is how the check box is placed at the beginning of each row.

<input type=\"checkbox\" name=\"checkbox[]\" id='' value=\"{$row['memberid']>\" />

2) I'm having issues capturing the values of which check boxes are checked. This code appears after the end table tag but before the end form tag

$checkBox = $_POST['checkbox'];
for($i=0; $i<sizeof($checkBox); $i++){
    $sus_id = $checkbox[$i];
    $sql = "UPDATE sometable SET somecolumn='1' WHERE id='$sus_id'";
    $result = mysql_query($sql);
    mysql_query($query) or die(mysql_error());
}
if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=somepage.php\">";
}

Any help would be most appreciated!

5
  • Do you "see" the checkboxes?I did not when i tried your code.. Commented Jan 4, 2012 at 4:41
  • 1. Can you post the actual PHP code, not just a stub? 2. Can you post the actual HTML code you've got? 3. Can you post var_dump($_POST['checkbox']); result? Commented Jan 4, 2012 at 4:45
  • I can post the code the builds the table, and yes, i see the check boxes :) Commented Jan 4, 2012 at 5:04
  • Col: Shrapnel, correct to initial statement... I see: array(1) { [0]=> string(16) "" } Commented Jan 4, 2012 at 5:26
  • Col: Shrapnel, the variable dump helped me see which values were being assigned to what array index. Thank you. Commented Jan 4, 2012 at 5:58

3 Answers 3

1

Try check the examples in the following link.

http://www.html-form-guide.com/php-form/php-form-checkbox.html

http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

And also mention different "name" for checkbox. Check the value is properly closed with braces {}and brackets(),[]..

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

4 Comments

The table is too big for these text areas,but i can paste some of it...while ($row = mysql_fetch_assoc($b_result)){ $usertable .= "<tr onmouseover=\"this.style.backgroundColor='#CDCDCD'; this.style.color='#000000';\" onmouseout=\"this.style.backgroundColor='#123456'; this.style.color='#FFF';\">"; $usertable .= "<td style=text-align:center;> <input type=\"checkbox\" name=\"checked[]\" id='' value=\"<?php echo $p_i;?>\" /></td>"; $usertable .= "<td> {$row['member_id']} </td>"; $usertable .= "<td> {$row['firstname']} </td>"; $usertable .= "<td> {$row['lastname']} </td>;
ugh, thats ugly... sorry still trying to get use to this interface.
I answered my own question, but i'm unable to update because my rating is < 100, here is what i said: ` $usertable .= "<td style=text-align:center;> <input type=\"checkbox\" name=\"checked[]\" id='' value={$row['member_id']}; /></td>"; `
Once the value attribute was fixed i was able to properly write the correct code to update my tables: ` $sql = "UPDATE members SET xIsActive=1 WHERE member_id=$sus_id "; $result = mysql_query($sql); } // if successful refresh admin page if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=administrate.php\">"; } mysql_close();`
0
<?php
if(isset($_POST['checkbox'])) {
    $checkBox = $_POST['checkbox'];
    for($i=0; $i<sizeof($checkBox); $i++){
        $sus_id = $checkbox[$i];
        //$sql = "UPDATE sometable SET somecolumn='1' WHERE id='$sus_id'";
        //$result = mysql_query($sql);
        //mysql_query($query) or die(mysql_error());
        echo "Value ==>"."$sus_id"."<br/>";
    }
}
?>

<form method=post action="">
    <input type="checkbox" name="checkbox[]" id='' value="1" /> 1
    <input type="checkbox" name="checkbox[]" id='' value="2" /> 2
    <input type="checkbox" name="checkbox[]" id='' value="3" /> 3
    <input type="submit" name="submit" value="22">
</form>

Also what you realy want to do by this line

mysql_query($query) or die(mysql_error());

Hope It Helps.

1 Comment

The sql stuff is code i'm adding to perform the suspends and activates on user accounts, but i prematurely posted that. Ignore it.
0

So i think i have figured it out further.

1) to answer the first question i asked:

$usertable .= "<td style=text-align:center;> <input type=\"checkbox\" name=\"checked[]\" id='' value={$row['member_id']} /></td>";

The rest worked itself out! Thanks!

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.