943,651 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5420
  • PHP RSS
Jun 8th, 2004
0

Fun with Forms

Expand Post »
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 "<input type=checkbox value=\"".$act_row['action_id']."\"> ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."<br>";
}
?>
[/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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sideshow Bob is offline Offline
18 posts
since Jun 2004
Jun 10th, 2004
0

Re: Fun with Forms

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?
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 10th, 2004
0

Re: Fun with Forms

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sideshow Bob is offline Offline
18 posts
since Jun 2004
Jun 11th, 2004
0

Re: Fun with Forms

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

Quote originally posted by SEO Bob ...

[php]
echo "<input type=checkbox value=\"".$act_row['action_id']."\"> ";
[/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:

Quote originally posted by SEO Bob ...

[php]

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

while($act_row = mysql_fetch_array($result)) {
echo "<input type=checkbox name="values[]" value=\"".$act_row['action_id']."\"> ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."<br>";
}
?>

[/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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 11th, 2004
0

Re: Fun with Forms

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sideshow Bob is offline Offline
18 posts
since Jun 2004
Jun 11th, 2004
0

Re: Fun with Forms

Quote originally posted by SEO Bob ...
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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 11th, 2004
0

Re: Fun with Forms

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

Re: Fun with Forms

Quote originally posted by SEO Bob ...
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]
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 11th, 2004
0

Re: Fun with Forms

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sideshow Bob is offline Offline
18 posts
since Jun 2004
Jun 13th, 2004
0

Re: Fun with Forms

That sounds fine. Maintaining it as an array gives you a lot of flexivity. You can manipulate it easily with the PHP array functions.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: making own webmail
Next Thread in PHP Forum Timeline: Error getting forms to send information





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC