473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CheckboxProblem

Hi all..I am coding a form in the process come across various irritating problems..due to which i'm hung..i cant go ahead..plz help anyone.

the different problems i'll discuss in different posts..so kindly have a look at them..and if possible plz help.

1)first is the checkbox problem..i want to check the multiple checkboxes at a time and want to update it after revisiting it.
Iam taking the name as name=type[]..in the form of array..but on checking it the result in the database simply gives the world 'array'.i want multiple checkboxes to be checked and updated simultaneously..
and the query to insert the checkbox value has to be included along with the query performed to insert data for rest of the form..

All of the forums i have visited have been of no use..so have a look at it.the coding is given below:

This is one of the parts of thee form and not the complete form
[PHP]<?

include "dbconnect.php";
//error_reporting('notice');
if(isset($_POST['submit']))
{//handle the form
$message=NULL;

$type=$_POST['type'];
$query ="insert into form
(type,other,purpose)VALUES('$type','$other','$purp ose');

$result =mysql_query($query);

if(!$result){die(mysql_error());}
else {echo 'Record Inserted';}



<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ><table width="90%" align=center cellpadding=2 class=medium>
<tbody>
<tr>
<td width="42%"><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox name=type[] value=Logo/Brochure>

Logo/Brochure </font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox value= Software/Web application development name=type[]>
Software/Web Application Development</font></td>
</tr>
<tr>
<td width="42%"><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox value=CD presentations name=type[]>
CD presentations</font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox value=2D/3D animations name=type[]>
2D/3D animations</font></td>
</tr>
<tr>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>

<input type=checkbox value= Web banner/intro name=type[]>
Web Banner/Intro</font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox value=E-learning name=type[]>
E-learning </font></td>
</tr>
<tr>
<td><p><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
If other, please explain: </font></p></td>
<td valign=bottom><div align=right><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif">
<input name="other" type="text" size="50" value="<?php if(isset($_POST['other'])) echo($_POST['other'])?>" >
</font></div></td>
</tr>
</tbody>
</table></form>
[/PHP]

So this is the code for checkbox...if any have the solution..plz post it
Jan 9 '08 #1
4 1244
stepterr
157 100+
Each time you do an array you need to have some sort of for loop to retrieve all of the values. Here is an example.

[PHP]<?php
if(isset($_POST['submit']))
{//handle the form
// $message=NULL;
$type ="";
for($i=0; $i<6; $i++)
{
$type .=$_POST['type'][$i];

}

$query ="insert into form
(type,other,purpose)VALUES('$type','$other','$purp ose');

$result =mysql_query($query);

if(!$result)
{
die(mysql_error());
}
else
{
echo 'Record Inserted';
}
}

?>[/PHP]
Jan 20 '08 #2
Each time you do an array you need to have some sort of for loop to retrieve all of the values. Here is an example.
Hi Stepterr.Thanx for your reply...I have managed to insert the checkbox checked data into mysql database.Below is the code for it.Now the main problem ,as i earlier mentioned is,how to make the checked status of the checkbox visible in the next page while updating the data.

I want that as i open the update/edit form the already cheked checkboxes be displayed..and later on i could update them by cecking on some other or the same checkbox.This updated data should also be updated i the datanase.
The same goes in case of Radiobuttons also.

I have managed to insert and update data for the entire form.Only checkbox and radiobutton are remaining.So..if u can guide me to the end of this solution..that would be great...Otherwise also it would be great [:)]
Here goes the code....

[PHP]

if(isset($_POST['submit']))
{//handle the form
$message=NULL;
$type=$_POST['type'];
$type = implode(",", $type);

$query ="insert into form (type) VALUES('$type');
$result =mysql_query($query);

if(!$result){die(mysql_error());}
else {echo 'Record Inserted';}
<form name="form" id="form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >

<table width="90%" align=center cellpadding=2 class=medium>
<tbody>
<tr>

<td width="51%"><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id=1 value="Logo/Brochure " name=type[0]>
Logo/Brochure </font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id="2" value= Software/Web Application Development name=type[1]>
Software/Web Application Development</font></td>
</tr>
<tr>
<td width="51%"><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id=3 value=CD presentations name=type[2]>
CD presentations</font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id=4 value=2D/3D animations name=type[3]>
2D/3D animations</font></td>
</tr>
<tr>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id=5 value= Web Banner/Intro name=type[4]>
Web Banner/Intro</font></td>
<td><font color="#333333" size="1"
face="Verdana, Arial, Helvetica, sans-serif"><br>
<input type=checkbox id=6 value=E-learning name=type[5]>
E-learning </font></td>
</tr>
[/PHP]

Here i have used array values like...type[0],type[1]....type[6].

using the same table and array now these checkboxes are to be updated..So please help...if you can..

Thanx and Regards
TechnoAtif
Jan 23 '08 #3
stepterr
157 100+
Hey TechnoAtif,
check out this thread that I had started a little bit ago dealing with the same issue. It should help guide you through what you need to do.
Jan 25 '08 #4
You take the checkbox in array that is correct and than when you (POST) submit multiple values from checkbox you will used for each loop not for like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. foreach ($_POST['checkboxname] as $chk)
  4. {
  5. $sql="insert into table (checkboxfield,field1,field2) values ('$chk','field1','field2')";
  6. //then execute the query
  7. }
  8.  
  9.  
and continue you code remember all the insert/update query written between for each loop.
Jan 25 '08 #5

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.