View Full Version : How do I do this..?

01-18-2008, 05:49 PM
I'm building a contact list using php. So far, I can view and edit contact details, but i'm stuck on the group assignment bit.

I've got a form which allows me to create a new group, that works fine, but the only problem is, how do I use the post variable? The group can have any name I want... how do I make it so the PHP code knows what group i'm referring to?



If it's not a static name, how do I get process it?

01-18-2008, 06:10 PM
Can you explain a little bit more how your script works? It's a bit hard to understand. The more details, the better we can help.

01-18-2008, 11:39 PM
The $_POST variable will retain the "value" of a form input as an array-item called the same as the form input's name. For example:

<input name="test" type="text" value="123" />

Would return $_POST['test'] with a value of "123".

Therefore, the selected "group" will be the value of your $_POST['group'] variable.

01-21-2008, 08:18 AM
Sorry I didnt explain it very well..

Ok, I've got a contact, Super Man.. I want him to be a member of the "Undies on the outside Gang", so I go and create that group. Now when I go to Super Man's page, I can tick the box next to "Undies on the outside Gang" in the form. Now when I click submit, I want to somehow record the fact that he's in the "Undies on the outside Gang" in my groupmem table. I know how to do the DB stuf, but I don't know how to get the information from the form.

With forms, I know how to use $_POST variables, but to use them, you have to know the name of the POST variable. ie 'undiesontheoutsidegang'. So how do I make it so that the PHP code knows the name of it?

I can't do:

if(isset($_POST['undiesontheoutsidegang'])) {

because what if the group name is different?

I don't know how to use a variable to do it since i'm submitting to another page. Do I need to use GET?

01-21-2008, 05:46 PM
You don't need to know the name. You should just have a field for each person in your DB that can have the group_id of the group they are associated with. Like so:


<select name="group">
<option value="" selected> Choose a group:</option>
//Get all groups
$x = mysql_query("SELECT group_id, name FROM groups");
while ($y = mysql_fetch_array)) {
$group_id = $y['group_id'];
$group_name = $y['name'];

//Echo each group as an option in the select box...then, $_POST['group'] will have the group_id that the user selected
<option value="<?=$group_id?>" <?php if($group_id == $group){echo'selected';}?>><?=$group_name?></option>


And the code at the top of the page would grab the group_id (which would be $_POST['group'] ) that the user selected, and then you can put that number in the DB with that person's entry. Make sense?

01-22-2008, 02:25 PM
Good response, but unfortunately a selection box is no good because I want them to be able to be a member of more than one group by using tickboxes, as I stated.

01-22-2008, 03:49 PM
Good response, but unfortunately a selection box is no good because I want them to be able to be a member of more than one group by using tickboxes, as I stated.

In that case, create a tickbox for each group_id, and make sure the name of all the tickboxes has [] after it (so they will get into an array). AKA:

<input type="checkbox" name="group[]" value="123" />
<input type="checkbox" name="group[]" value="124" />

Then, at the top of your page, put the $_POST['group'] into a variable, and do a foreach() to grab each number and add them to your DB with that user's user_id. That's the basic gist of what you need. If you need more help, just let us know. :)

01-22-2008, 05:09 PM

You can do that!!? Wicked :))) Thanks, i'll have a tinker with it.. :)