Submit form with array of check boxes - PHP example
By Sergey Skudaev
![]()
![]()
![]()
![]()
Recently, I used HTML form with array of check boxes. Previously, I wrote about ussing array of text input type. It is easy: on the form you just add brakets to the input text name fields.
print('<form name="arrayoffields" method="post" action="act_submit_check.php">');
for($i=0; $i<$count; $i++)
{
print('<input type="text" name="lastname[]" size="15"/>');
print('<input type="checkbox" name="confirm[]" />');
}
print('<input type="hidden" name="count" value="'.$count.'"/>');
print('<input type="submit" name="submit" value="Submit"/>');
print('</form>');
On act_submit_check.php page you write:
$lastname=array();
$confirm=array();
$count=0;
if(isset($_POST['lastname']));
$lastname=$_POST['lastname'];
if(isset($_POST['confirm']));
$confirm=$_POST['confirm'];
if(isset($_POST['count']));
$count=$_POST['count'];
for($i=0; $i<$count; $i++)
{
$sql="insert into mytable(name, confirm) values
('".$lastname[$i]."', '".$confirm[$i]."')";
....some code........
}
Problem with this code is that in array of check boxes an element of array exists only if check box is marked. For example, you have on your form 10 text boxes and 10 check boxes. Only the first, the fourth and the seventh text box is filled and only the first, the fourth and the seventh check box is marked. All the rest fields are blank.
The lastname array will have size of 10, but the confirm array of check boxes will have size of 3, because only 3 check boxes are marked. Visitor confirmed the first, the fourth and the seventh records, but in database wrong data will be inserted because the first, the second and the third element of checkboxes array will have value and no more elements of check boxes array will exist.
To fix the problem you have to assign an index and a unique value to each check box field. I use count.
print('<form name="arrayoffields" method="post" action="act_submit_check.php">');
for($i=0; $i<$count; $i++)
{
print('<input type="text" name="lastname[]" size="15"/>');
print('<input type="checkbox" name="confirm['.$count.']" value="'.$i.'"/>');
}
print('<input type="hidden" name="count" value="'.$count.'"/>');
print('<input type="submit" name="submit" value="Submit"/>');
print('</form>');
Now your array of check boxes has 10 elements even if not all of them are marked.
for($i=0; $i<$count; $i++)
{
$markconfirm=0;
if($confirm[$i] !="")
$markconfirm=1;
$sql="insert into mytable(name, confirm) values ('".$lastname[$i]."', '".$markconfirm."')";
if(!mysql_query($usql))
{
echo mysql_errno() . ": ";
echo mysql_error() . "<br>";
}
else
echo "Record inserted";
That is how you have to use array of check boxes on HTML form.
Please rate the tutorial
Learn SQL Programming By Examples [Kindle Edition]2.99
Learn PHP Programming by Examples [Kindle Edition] $2.99
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49
New-trip.com website source code
| Comments | |
|---|---|
- Home
- arrayof_check_boxes
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- PHP Code Examples
- Learn PHP
- Modeless Popup
- Read Screen Resolution
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- GET mess
- Array of Check boxes
- PHP and MS Access
- User_Auth. Demo
- PHP App Example
- PHP + Oracle 1
- PHP + Oracle 2
- Image Collection
- Create Gallery
- Block Spam Post
- $_REQUEST,$_SESSION
- Headers Allredy Sent
- Virtual Server
- Apache:OS 10048
- Set Shopping Cart
- My Best CSS Layout
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Site map
- Registration
Web programming Tips