Jump to content

Checkbox array


Donovan

Recommended Posts

I have a form with several records displayed in each row.  There are only one input field per record.  It is a simple checkbox to update the status of each record.  The form submits to itself.

 

echo "<form action='".$_server['php_self']."' method='post'>";

 

My checkbox is here:

<td align="center">Complete: <input type="checkbox" name="status[{Session_ID}]" value="1" <?php if($status == "1"){echo " CHECKED";}?>>  

 

There are two values that I would need from this.  The Session_ID for this record and the value of status (the checkbox).

 

I click Update here:

echo "<td colspan=\"7\" align=\"center\"><input type=\"Submit\" name=\"Update\" value=\"Update\"></td>";

 

..and then check for the value of _POST when the page refreshes.

 

if(isset($_POST["Update"]) && ""!=$_POST["Update"]) { 
foreach($_POST['status'] as $Key => $value) { 
$setstatus = $db->sql_query("UPDATE ".$prefix."_tl_session SET status ='$value' WHERE Session_ID = '$Key'");
//die('<br />$setstatus = '.$setstatus.'<br />'); 
if (!$setstatus) {
	echo("<p>Error performing query: " . mysql_error() . "</p>");
	exit();   
	}
}
}

 

It's not working yet and can anyone tell me where I went wrong?

 

I was getting the interpreted value of $setstatus here

die('<br />$setstatus = '.$setstatus.'<br />'); 

 

and it was giving me  $setstatus = 1

 

The page I have has three records on it and I try to update the second one, but it looks like the last one is getting updated and getting set to 1.

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/94494-checkbox-array/
Share on other sites

This line looks to be your problem:

<td align="center">Complete: <input type="checkbox" name="status[{Session_ID}]" value="1" <?php if($status == "1"){echo " CHECKED";}?>>  

 

You probably want to echo the PHP variable $Session_ID. So it would be:

<td align="center">Complete: <input type="checkbox" name="status[<?php echo $Session_ID; ?>]" value="1" <?php if($status == "1"){echo " CHECKED";}?>>  

 

To confirm it's working, do a View Source on the generated page and make sure the value showing up there.

 

If that doesn't work, then post the loop around that line so I can see what you are setting your variables to.

 

Link to comment
https://forums.phpfreaks.com/topic/94494-checkbox-array/#findComment-483905
Share on other sites

Here is the view source:

 

<td width='15%'>DISEASES OF BLOOD VESSELS & HEART</td>
    <td align="center"><input type="checkbox" name="status[46]" value="1" > 	
    <td align='center'><nobr>[ <a href='admin.php?op=TLSessionEdit&Session_ID=46&Course_ID=CL00000011'>Edit</a> | <a href='admin.php?op=TLSessionRemove&Session_ID=46'>Delete</a> ]</nobr></td><td align='center'>
<a href='admin.php?op=TLViewIratGrades&Session_ID=46'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLViewGratGrades&Session_ID=46'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>

<a href='admin.php?op=TLViewAppExGrades&Session_ID=46'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>
<td align='center'>
<a href='admin.php?op=TLSessionIratGrades&Session_ID=46'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLSessionGratGrades&Session_ID=46'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>
<a href='admin.php?op=TLSessionAppExGrades&Session_ID=46'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>
</tr>
<tr><td width='10%'><img src='modules/Team_Learning/images/options.png'> Cardio TL#2</td>
<td width='10%'>2008-01-17</td>
<td width='15%'>Pharmacology</td>
    <td align="center"><input type="checkbox" name="status[47]" value="1" > 	
    <td align='center'><nobr>[ <a href='admin.php?op=TLSessionEdit&Session_ID=47&Course_ID=CL00000011'>Edit</a> | <a href='admin.php?op=TLSessionRemove&Session_ID=47'>Delete</a> ]</nobr></td><td align='center'>

<a href='admin.php?op=TLViewIratGrades&Session_ID=47'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLViewGratGrades&Session_ID=47'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>
<a href='admin.php?op=TLViewAppExGrades&Session_ID=47'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>
<td align='center'>
<a href='admin.php?op=TLSessionIratGrades&Session_ID=47'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLSessionGratGrades&Session_ID=47'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>
<a href='admin.php?op=TLSessionAppExGrades&Session_ID=47'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>
</tr>
<tr><td width='10%'><img src='modules/Team_Learning/images/options.png'> Cardio TL#3</td>
<td width='10%'>2008-01-24</td>
<td width='15%'>ECKOCARDIOGRAM</td>

    <td align="center"><input type="checkbox" name="status[48]" value="1" > 	
    <td align='center'><nobr>[ <a href='admin.php?op=TLSessionEdit&Session_ID=48&Course_ID=CL00000011'>Edit</a> | <a href='admin.php?op=TLSessionRemove&Session_ID=48'>Delete</a> ]</nobr></td><td align='center'>
<a href='admin.php?op=TLViewIratGrades&Session_ID=48'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLViewGratGrades&Session_ID=48'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>
<a href='admin.php?op=TLViewAppExGrades&Session_ID=48'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>
<td align='center'>
<a href='admin.php?op=TLSessionIratGrades&Session_ID=48'><img src='modules/Team_Learning/images/irat.gif' height='16' width='16' border='0' alt='IRAT' title='IRAT'></a>
<a href='admin.php?op=TLSessionGratGrades&Session_ID=48'><img src='modules/Team_Learning/images/grat.gif' height='16' width='16' border='0' alt='GRAT' title='GRAT'></a>

<a href='admin.php?op=TLSessionAppExGrades&Session_ID=48'><img src='modules/Team_Learning/images/appex.gif' height='16' width='16' border='0' alt='AppEx' title='AppEx'></a>
</td>

 

You can see the value for status from these three lines

 

<td align="center"><input type="checkbox" name="status[46]" value="1" > 
<td align="center"><input type="checkbox" name="status[47]" value="1" > 
<td align="center"><input type="checkbox" name="status[48]" value="1" > 	

 

It is still writing the last record out of the three even though I clicked the first or second checkbox.

 

I am unsure what you mean here:

 

If that doesn't work, then post the loop around that line so I can see what you are setting your variables to.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/94494-checkbox-array/#findComment-483925
Share on other sites

Ok, so the two checkboxes you check are updating to 1? If so that is good.

 

Unchecked checkboxes are not passed when the form is submitted. So if you want unchecked checkboxes to UPDATE to a status of 0 or NULL, you will have to do something else. Would it be logical to mark all records with a status of 0 (or NULL) and then update the checked ones to 1 every time the page is submitted? You won't be able to do this if only some records are shown.

Link to comment
https://forums.phpfreaks.com/topic/94494-checkbox-array/#findComment-483953
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.