Getting Check box values using array in PHP

 John  Mike  Rone
 Mathew  Reid  Simon
Many times we have to display a list of names or records for consideration and out of which user can select any number of records for further action. Here we may not know exactly how many records will be displayed as based on different conditions records will be collected from a table.

We can list the records with a checkbox by the side of it and user can select any number of recodes. We will have one unique id associated with each record and that will be used as value of the check box. Here is the code to display a list and then you can select and submit to see the result.
$box=$_POST['box'];
$str='';
foreach($box as $key => $val) {
$str=$str. "$val,";
}
$str=rtrim($str,','); // To remove last char using rtrim
echo "<pre><code>$str</code></pre>"; 


echo "<form method=post action=''>";
echo "<table border='0' 
cellspacing='0' style='border-collapse: collapse' width='100' >

<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=box[] value='John'></td>
<td width='25%'>&nbsp;John</td>
<td width='25%'><input type=checkbox name=box[] value='Mike'></td>
<td width='25%'>&nbsp;Mike</td>
<td width='25%'><input type=checkbox name=box[] value='Rone'></td>
<td width='25%'>&nbsp;Rone</td>
</tr>
<tr bgcolor='#f1f1f1'>
<td width='25%'><input type=checkbox name=box[] value='Mathew'></td>
<td width='25%'>&nbsp;Mathew</td>
<td width='25%'><input type=checkbox name=box[] value='Reid'></td>
<td width='25%'>&nbsp;Reid</td>
<td width='25%'><input type=checkbox name=box[] value='Simon'></td>
<td width='25%'>&nbsp;Simon</td>
</tr>

<tr><td colspan =6 align=center><input type=submit 
value=Select></form></td></tr>
</table>";
This is a server side solution and we can get the array value using client side JavaScript we can also get the value of the array of checkboxes.
Note that if in your server settings global variables is set to OFF then you have to take care for that.

To remove one extra , at the end of the output we used rtrim function.
In ASP similar script to collect checkbox values of a form is available here.
Array REFERENCE
PHP Form Managing Radio button using PHP

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    smo1234

    18-06-2012

    Good one thanks

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer