Forum Moderators: coopster

Message Too Old, No Replies

newbie checkbox question

how are checkboxes viewed?

         

Baruch Menachem

7:12 am on Jul 5, 2008 (gmt 0)

10+ Year Member



a little code snippet

$subselect="select * from topics order by seekwense";
$topicss=mysql_query($subselect) or die( mysql_error());
$ilcv=0;
while ($row1=mysql_fetch_array($topicss))
{
$thissequence=$row1['seekwense'];
$thislabel=$row1['disscussion'];
echo"<label for $thislabel> $thislabel</label>\n";
echo"<input type='checkbox' name='topic' value=$thissequence id=$thislabel />\n";

$ilcv++;

if ($ilcv>=4)
{
echo"</p><p>";

$ilcv=0;
}
}


essentially I get a list of topics from a data table. After each post, I am going to save the topics in that post to a data table that will have the structure post#,thissequence so I can go back and review any postings that contain that topic.

Anyway... say I have 20 topics, and I check off anywhere from 1 to 20 boxes. Does topic store them as an array of true /false items in a array? When preparing to save my post the data tables, can I cycle through the array and store the checkboxes that were checked?

Is it possible to have a checkbox that says "go ahead and ad another?"

Thanks

siMKin

7:53 am on Jul 5, 2008 (gmt 0)

10+ Year Member



what you should do is format the input fields like this:

<input type="checkbox" name="somename[1]">
<input type="checkbox" name="somename[2]">

this will create an array of variables in $_POST which is visible when you do

var_dump($_POST['somename'])

mind, that only the checkboxes that are checked will appear in this array. the ones without checkmark will simply not exist in that array.

Baruch Menachem

4:05 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



That is almost exactly what I think I am looking for. Thanks.

I basicly am trying to create tags for the posts. this may be the wrong way to invent the wheel. is there a better way to do tags?

eelixduppy

3:27 am on Jul 7, 2008 (gmt 0)



I'm not sure what you still are looking for from this. Try the following in your code:

echo '<input type="checkbox" name="topic[]" value="'.$thissequence.'" id="'.$thislabel.'" />'."\n";

Notice how I changed the name to topic[] and also correctly added the quotes around each of the attributes. On the action page try the following:


echo '<pre>'; print_r($_POST['topic']); echo '</pre>';