Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts

    How do I do this..?

    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?

    ie

    PHP Code:

    if(isset($_POST['groupwhichicreated'])) 
    If it's not a static name, how do I get process it?
    Last edited by samuurai; 01-18-2008 at 05:56 PM. Reason: brevity

  • #2
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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.

  • Users who have thanked JohnDubya for this post:

    samuurai (01-24-2008)

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    <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.

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:

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

    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?
    Last edited by samuurai; 01-21-2008 at 04:01 PM.

  • #5
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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:

    PHP Code:
    <form>

    <select name="group">
       <option value="" selected> Choose a group:</option>
    <?php
    //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>
    <?php
    }
    ?>
    </select>

    </form>
    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?
    Last edited by JohnDubya; 01-21-2008 at 05:54 PM.

  • Users who have thanked JohnDubya for this post:

    samuurai (01-24-2008)

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by samuurai View Post
    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:

    PHP Code:
    <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.

  • Users who have thanked JohnDubya for this post:

    samuurai (01-24-2008)

  • #8
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JohnDubya View Post
    name="group[]"
    You can do that!!? Wicked )) Thanks, i'll have a tinker with it..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •