954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Fun with Forms

Hi, I'm creating an administration area for a little web-app and I've come across a problem that I've never encountered before.

The form that I'm working on is used for the maintenance of a table that has several properties. One of these properties can be selected with checkboxes, and then recorded in the correct column in a comma-delimited list. The problem that I'm having is constructing/alterring the list - for some reason I can't for the life of me figure out how to do it.

Oh yeah, the fun part is that the properties that are in the checkbox are created on the fly from another table, which is why i didn't decide to create a column for each option and flag it.

Here's the snippet of code that creates the checkbox area:
[php]
<?php
$sql2 = "SELECT * FROM tbl_action";
$result = mysql_query($sql2);

while($act_row = mysql_fetch_array($result)) {
echo " ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."
";
}
?>
[/php]

Ok, 2 tables - spy and action. All columns from spy are called spy_name, and from action are act_name.

So far I've considered using Javascript, making the code create a new column every time a new action is created, and various other techniques that have been thrown out as soon as I think of them.

This is the last part of the admin area, and once it's complete I'll be a very happy boy.

Sideshow Bob
Newbie Poster
18 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

I don't think I understand you correctly. All you want to do is check multiple checkboxes, put those checked values in a comma delimited string, and insert it in some table?

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

Hi inscissor, thanks for the reply. Let me see if I can clarify myself.

I want to be able to check/uncheck multiple checkboxes, and put those values in a comma delimited string, and then stick it in a column, that's right.

The part that's throwing me for a loop though is that the checkboxes are created from the database (the options come from another table) in the first place.

I'm starting to think I'm overthinking this whole problem, but still haven't figured out how to do it.

Sideshow Bob
Newbie Poster
18 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

Well, your problem seems to stem from how you're creating the checkboxes in the loop. First of all, here:


[php]
echo " ";
[/php]you don't create a name for the checkbox, so how are you going to retrieve it? The way it's usually done is you give the checkbox a name with open and closed bracks, like this: values[]

So when you pass this over, it'll pass it over as an array. So this is all you have to do:
[php]

<?php
$sql2 = "SELECT * FROM tbl_action";
$result = mysql_query($sql2);

while($act_row = mysql_fetch_array($result)) {
echo " ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."
";
}
?>

[/php]So when you submit this, it'll submit an array of the values of only the ones that got checked. So you loop through this array, in this case being values[]. Also, when you're retrieving this form variable, retrieve it as $_POST['values'] and not $_POST['values[]']. I'm sure you can do the rest. Anything else, let me know.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

Thanks Inscissor,

To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.

Sideshow Bob
Newbie Poster
18 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 
Thanks Inscissor, To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.

Were you able to figure it out? I do this all the time, so it's no sweat. I'll give you code if you want.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

Given that the data is stored in an array, what type of structure should I use for storing it?

Sideshow Bob
Newbie Poster
18 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 
Given that the data is stored in an array, what type of structure should I use for storing it?

Didn't you say you wanted it in a comma delimited string? So your structure would be a string. All you would need to do is loop through the array, and as you're looping, append\concatenate the adjacent strings in the array. Fortunately, you could also use the implode() function, so you'll have something like this:

[php]
$values = $_POST['value']; // getting value array from form variable
$delimited_values = implode(",", $values);
echo $delimited_values;
[/php]

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

It actually doesn't matter if they're stored in a comma-delimited string or not (but yes, i did say that that's how i wanted it).

My only requirement is that the data can be retrieved and written to easily. I've been looking at serializing the array and putting it into the column, but if you have another recommendation...

Sideshow Bob
Newbie Poster
18 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

That sounds fine. Maintaining it as an array gives you a lot of flexivity. You can manipulate it easily with the PHP array functions.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You