Hi everyone,
I'm having a problem on passing values from my combo box. Let me show you first my code. I have 2 files test.php and view.php.
This is what's inside the test.php

while($row = mysql_fetch_array($strSQL)){
                $data .= "<tr>";
                $data .= "<td>";
    $data .= "<input type='checkbox' class='memberchk' name='membersID[]' value='".$row['member_id']."' />";
    $data .= "</td>";
        $data .="<td>";
        $data .= "".$row['name'];
        $data .="</td>";
        
        $data .="<td>";
        $data .= "<select name='access[]'>";
                
                if($row['access'] == 'member'){
			$data .="<option selected value='member'>Member</option>";
		}
		else{
			$data .="<option value='member'>Member</option>";
		}
		
		if($row['access'] == 'admin'){
			$data .="<option selected value='admin'>Admin</option>";
		}
		else{
			$data .="<option value='admin'>Admin</option>";
		}
	$data .="</select>";
        $data .="</td>";
        $data .= "</tr>";
}

This is my view.php

$member_id = $_POST['membersID'];
$access = $_POST['access'];
$size = sizeof($member_id);

for($i=0;$i<$size;$i++){
echo "Member id: ".$member_id[$i]."</br>";
echo "Access: ".$access[$i];
echo "</br>";
}

In this code is just a display on every values of my $access and $member_id array variables
These are the values in my database
member_id | names |access|
---------------------------------------------------
1 |Jim | member |
2 |Elson | admin |
3 |Jezel | member |
4 |Rose | admin |
----------------------------------------------
As you can see on the codes above, it has a checkbox and a combo box, and everytime I click on the button update, it will display values on what's inside the $access and $member_id array variables.
My problem here is that every time I change Jezel as an admin, the $access = $_POST will still display member,
But if I try to change Jim as an admin, $access = $_POST will display admin, there will be no problem..
There's also another problem, I need to checked all the checkboxes on the list, so that Jezel's access will changed.
What's the problem with this?
By the way, $access and $member_id is an array.

I was really having a hard time explaining this problem. Just ask me if there's something unclear to you regarding on my explanation. Thanks!
jimgym1989
Forum Newbie

Posts: 6
Joined: Thu May 14, 2009 9:31 am

The issue you are facing, is that checkboxes only transfer the values when they are checked. For e.g., in the case you have above, your for loop only goes one look where $size takes the value of 1 in view.php but the $access has a array length of 4 and the value you actually need is in the 3rd position.

To make this more clear, in the example you stated above would be you are selecting Jezebel and changing the member access to admin

Member ID array -> member_id[0]=3
Access array -> access[0]=member, access[1]=admin, access[2]=admin, access[3]=admin

when you should actually have had

Member ID array -> member_id[0]=1,member_id[1]=2,member_id[2]=3, member_id[3]=4
Access array -> access[0]=member, access[1]=admin, access[2]=admin, access[3]=admin


When 0 matches 0 index above you will get member as the access type when actually it should have been member_id[2] which should match access[2]. I hope this makes things clear. In order to correct this, you would need to transfer all the checkbox values which unfortunately HTML or PHP does not do on its own, so you need to create a serialization to help it along.

Try the following code.

$i=0;
while($row = mysql_fetch_array($result)){
    $data .= "<tr>";
    $data .= "<td>";
    $data .= "<input type='checkbox' class='memberchk' name='serialID[]' value='$i-{$row['member_id']}' />";
    $data .= "</td>";
        $data .="<td>";
        $data .= "".$row['names'];
        $data .="</td>";
        
        $data .="<td>";
        $data .= "<select name='access[]'>";
                
                if($row['access'] == 'member'){
			$data .="<option selected value='member'>Member</option>";
		}
		else{
			$data .="<option value='member'>Member</option>";
		}
		
		if($row['access'] == 'admin'){
			$data .="<option selected value='admin'>Admin</option>";
		}
		else{
			$data .="<option value='admin'>Admin</option>";
		}
	$data .="</select>";
    $data .="</td>";
    $data .= "</tr>";
    $i++;
}

In the above, I have changed the member_id to a serial_id which we will be using in the view.php file. This will help "pad" the checkboxes later. So we would actually be transferring the serial position, as well as the member_id.

In the view.php file we would need

$serial_arr = $_GET['serialID'];
$member_id = array();
foreach($serial_arr as $value){
	$serial = explode("-",$value);
	$member_id[$serial[0]]=$serial[1];
}
$access = $_GET['access'];
$size = sizeof($access);

for($i=0;$i<$size;$i++){
	if ($member_id[$i]!=""){
		echo "Member id: ".$member_id[$i]."</br>";
		echo "Access: ".$access[$i];
		echo "</br>";
	}
}

In the above, you will notice that I have added a foreach loop to explode the serial array and create a new member array to take care of PHP just transferring only the checked values. This should make sure the 0 matches 0 index for Jim or 2 matches 2 index for Jezebel.

Try it out and let me know if you have any questions. All the best!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.