By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

How to get the data from checkboxes

P: 25
Hi,How to get the data from checkboxes from php .code is below
Expand|Select|Wrap|Line Numbers
  1. <form action="someform.php" method="get">
  2. <TABLE border="2">
  3. <TR>
  4.     <TD>Name:
  5.     <TD> 
  6.  
  7. </TR>
  8. <TR>
  9.     <TD>Password: 
  10.     <TD>
  11.  
  12. </TR>
  13. <TR>
  14.     <TD>Age:
  15.     <TD>              Enter your life story here
  16. </TR>
  17.  
  18. <TR>
  19. <TD> Tennis
  20. <input type="radio" name="FaveSport" value="Cricket"> Cricket
  21. <input type="radio" name="FaveSport" value="Baseball"> Baseball
  22. <input type="radio" name="FaveSport" value="Polo"> Polo
  23.  
  24. </TD>
  25. <TD>
  26. <input type="checkbox" name="Languages[]" value="PHP" CHECKED> PHP
  27. <input type="checkbox" name="Languages[]" value="CPP"> C++
  28. <input type="checkbox" name="Languages[]" value="Delphi"> Delphi
  29. <input type="checkbox" name="Languages[]" value="Java"> Java
  30.  
  31. </TD>
  32. </TR>
  33. <TR>
  34.     <TD>
  35.     <TD>
  36.  
  37. </TR>
  38. </TABLE>
  39.  
  40. </form>
Aug 6 '07 #1
Share this Question
Share on Google+
6 Replies


nathj
Expert 100+
P: 938
If you give you checkboxes all subtly different names - laguage1, language2 etc then you can loop through them using a for loop and retreive their value that way.

One thing I noticed from you code is that you are using the GET method. I would recommend changing this to use POST. Anything submited with the GET method loads the values into the URL as part of the query string. When taking data from a user it is better to use the POST method as the data is then hidden from view.

In answer to your question a for loop should do the trick.

Have a go and if you get stuck post back and I'll try to help further.

Cheers
nathj
Aug 6 '07 #2

ak1dnar
Expert 100+
P: 1,584
Hi There,

Hope this may help to you .

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $chk_box_array = $_POST['Languages']; // *uHmmm* I am using Post method Here.
  3. //print_r($chk_box_array); // If you want to see the array uncomment this line
  4. $imploded_array = implode(',',$chk_box_array);
  5. echo $imploded_array;
  6. ?>
  7.  
So, As you can see its an Array,
Here After You Can Use the Array Elements, As per your Requirement.
Post back, If doesn't work.
Aug 6 '07 #3

P: 3
Hi There,

Hope this may help to you .

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $chk_box_array = $_POST['Languages']; // *uHmmm* I am using Post method Here.
  3. //print_r($chk_box_array); // If you want to see the array uncomment this line
  4. $imploded_array = implode(',',$chk_box_array);
  5. echo $imploded_array;
  6. ?>
  7.  
So, As you can see its an Array,
Here After You Can Use the Array Elements, As per your Requirement.
Post back, If doesn't work.

I guess what i would want to do instead of echo'ing the array if i wanted to insert the selected items into a database would be a for loop basically...?


[PHP]<?php
$Languages=array($chk_box_array);

foreach ($Languages as $value)
{
insert mySQL command goes here...
}
?>
[/PHP]
what i am trying to do, is allow people to select multiple pictures and say if a relative is in it, then store the relation in a 3rd table...

table 1 - familymembers (name, address, blah blah blah)
table 2 - pictures (fileName, date, blah)
table 3 - relations (familymembers.name, pictures.filename, blah, blah)

obviously the displaying and searching i'm still working on, but the forms are where i'm having issues, I can get the data i need to display the form properly, by a simple select msg in a while loop for a check boxed form generated based on the pictures table.

[PHP]
$result = mysql_query("SELECT * FROM pictures");
while($row = mysql_fetch_array($result))
{
echo "<!--START ROW FOR " .$row['fileName']."-->
<input type=checkbox name=picture value=".$row['fileName']." />
<!-- END ROW FOR " .$row['fileName']."-->";
}
[/PHP]

but getting that form submitted has proven to be a nightmare, I don't want them to have to use a radio where it's just one picture selected, i'd much rather have them be able to select multiple pictures that relate to one person... then have it insert into the relations table for the pictures file name for the checkbox field.

Where i am having trouble is handling the array on the insert side, do i basically just invert my above statement? kind of lost here... any help?
Aug 7 '07 #4

ak1dnar
Expert 100+
P: 1,584
I guess what i would want to do instead of echo'ing the array if i wanted to insert the selected items into a database would be a for loop basically...?


[PHP]<?php
$Languages=array($chk_box_array);

foreach ($Languages as $value)
{
insert mySQL command goes here...
}
?>
[/PHP]
what i am trying to do, is allow people to select multiple pictures and say if a relative is in it, then store the relation in a 3rd table...

table 1 - familymembers (name, address, blah blah blah)
table 2 - pictures (fileName, date, blah)
table 3 - relations (familymembers.name, pictures.filename, blah, blah)

obviously the displaying and searching i'm still working on, but the forms are where i'm having issues, I can get the data i need to display the form properly, by a simple select msg in a while loop for a check boxed form generated based on the pictures table.

[PHP]
$result = mysql_query("SELECT * FROM pictures");
while($row = mysql_fetch_array($result))
{
echo "<!--START ROW FOR " .$row['fileName']."-->
<input type=checkbox name=picture value=".$row['fileName']." />
<!-- END ROW FOR " .$row['fileName']."-->";
}
[/PHP]

but getting that form submitted has proven to be a nightmare, I don't want them to have to use a radio where it's just one picture selected, i'd much rather have them be able to select multiple pictures that relate to one person... then have it insert into the relations table for the pictures file name for the checkbox field.

Where i am having trouble is handling the array on the insert side, do i basically just invert my above statement? kind of lost here... any help?
I think your requirement is entering this (May be a Form) check box values to the RELATIONS table.


[PHP]
$result = mysql_query("SELECT * FROM pictures");
while($row = mysql_fetch_array($result))
{
echo "<!--START ROW FOR " .$row['fileName']."-->
<input type=checkbox name=picture value=".$row['fileName']." />
<!-- END ROW FOR " .$row['fileName']."-->";
}
[/PHP]

But, I couldn't figure out any Table Relationship under this Query to do such a thing.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM pictures
Aug 7 '07 #5

P: 3
I think your requirement is entering this (May be a Form) check box values to the RELATIONS table.


[PHP]
$result = mysql_query("SELECT * FROM pictures");
while($row = mysql_fetch_array($result))
{
echo "<!--START ROW FOR " .$row['fileName']."-->
<input type=checkbox name=picture value=".$row['fileName']." />
<!-- END ROW FOR " .$row['fileName']."-->";
}
[/PHP]

But, I couldn't figure out any Table Relationship under this Query to do such a thing.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM pictures

that's because i want it to show all the pictures, the only thing i'm actually using from the select (and the array) at this time is the $fileName and $picTitle which i use to get the picture to be displayed.

[PHP]
$counter = 0;
$result = mysql_query("SELECT * FROM pictures");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
$counter = $counter + 1;
$output .="<!--START ROW FOR checkbox " .$row['fileName']." 3 collums |10| 120 | 10|-->";
$output .=" <td width=10 id=left10 align=left >".$counter."";
$output .=" </td>";
$output .=" <td width=120 id=middle align=center >";
$output .=" <h4>".$row[picTitle]."</h4><br>";
$output .=" <a href=".$fileURL."".$row['fileName']." ><img src=".$fileURL."THUMBS/".$row['fileName']." ></a><br>";
$output .=" <input type=checkbox name=PICTURES value=".$fileURL."".$row['fileName']."/>";
$output .=" </td>";
$output .=" <td width=10 id=right10></td>";
$output .="<!-- END ROW FOR checkbox " .$row['fileName']."-->";
if ( $counter % 3 == 0 )
{
$output .= "</tr><tr>";
}
}[/PHP]


so it makes a table from the pictures and adds a check box below each picture, this pictures checkbox has it's file name as it's value.

if they select more than one check box I want to be able to accept all those values into an array so that i can insert them into a third (relations) table

so i can list them as

person A is in pictures 1, 2, 3, 6, 89, 5465, 32, 213, etc.. see?

I am really interested in figuring out how to get it from this form than anything, i mean does it format the Variable as an array? do i need to designate this?
Aug 8 '07 #6

P: 3
Scratch the above, i wasn't thinking correctly. that's an older version of the form That i have. I read your note above making a languages[1], [2]... version for the checkboxes is a good idea. I've never made a for loop in php...
[PHP]
$counter= 0 ;
$result = mysql_query("SELECT * FROM pictures");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
$counter = $counter + 1;
$output .="<!--START ROW FOR checkbox " .$row['fileName']." --> ";
$output .=" <td width=10 id=left10 align=left ><!--".$counter."--><br></td>";
$output .=" <td width=120 id=middle align=center valign=top >";
$output .=" <a href=".$fileURL."".$row['fileName']." ><img src=".$fileURL."THUMBS/".$row['fileName']." ></a><br>";
$output .=" <input type=checkbox name=PICTURES[".$counter."] value=".$row['fileName']." />".$row[picTitle];
$output .=" </td>";
$output .=" <td width=10 id=right10 ><br></td>";
$output .="<!-- END ROW FOR checkbox " .$row['fileName']." -->";
if ( $counter % 4 == 0 )
{
$output .= "</tr><tr>";
}
}
$output .="</tr>";[/PHP]

so, this is the part of the form that selects the pictures.

here's the form for the persons.
[PHP]
$result = mysql_query("SELECT * FROM persons");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
$counter = $counter + 1;
$output .="<!-- START ROW FOR Radio " .$row['name']."--> ";
$output .=" <td width=10 id=left10 align=left valign=top >".$counter.". ";
$output .=" </td>";
$output .=" <td width=120 id=middle align=left valign=top >";
$output .=" <input type=radio name=PERSONS value=".$row['name']." >".$row['name'];
$output .=" </td>";
$output .=" <td width=10 id=right10 valign=top ></td>";
$output .="<!-- END ROW FOR Radio " .$row['Name']."--> ";
if ( $counter % 1 == 0 )
{
$output .= "</tr><tr>";
}
}
$output .="</tr>";[/PHP]

the forms sql statement is generated when i do an include for these two files. this is because i want to use these loops in different spots on the site, so instead of making one big form for each of these two items i made them separated.

-anyways-
my real beef is on how i get the forms data to be looked at based on the picture, i need to check to see if they've selected images. and i don't know how to see if the (or any) check box(es) has been checked. I need to know if the check box(es) is checked... does it load the variables Value if it is?

something like

$_POST[PERSONS[1]] would be the first pics file name if it was selected right?

if it is selected then insert the record else if it's not, then reload the page again.
... (any help?) something like below
[PHP]
foreach $ii in $_POST[PICTURES[$ii]
{
if (isset($_POST[PERSONS[$ii])) //not null values
{
$sql = mysql_query("INSERT INTO relations ('fileName', 'person')
VALUES ('.$_POST[PICTURES[$ii].', '.$_POST[PERSONS].')");
if (!mysql_query($sql,$con))
{
die('SQL Error: ' . mysql_error());
}
}
else //value of persons[ii] is null
{
echo"no pics selected please try again.";
include ('displaymainform.php');
}
}[/PHP]
Aug 9 '07 #7

Post your reply

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