SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re-ordering categories design help

    The general need:
    To have a select list which allows re-ordering of the items in it and can be saved/updated using PHP to a MySQL database.

    What's been done:
    I have the select list working like so:
    <select name='my_list' size='20'>
    ...options...
    </select>

    I also have two buttons with javascript attached to them to move around selected options (i.e., you can click on an option, press the "up" button and it will move the option up one in the list).

    The problem:
    When all the re-ordering is said and done, I need to update a database with the options and their final order on screen. My problem is that hitting a submit button will either send one option value to the form or none (depending if one was selected or not). What I need to know is all the options in their current order state on the screen.

    What would be the best way to accomplish this? I toyed with the idea of having javascript run when the submit button is pressed to "select" all the items in the list and then submit the form. That will pass all the options to the form and they'll be available. This doesn't sound very clean though.

    Can anyone suggest a better approach to solve this need?

  2. #2
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <form method="post" action="result.php">
    <
    select size="3" multiple="multiple" name="numbers[]">
    <
    option value="a">One</option>
    <
    option value="b">Two</option>
    <
    option value="c">Three</option>
    <
    option value="d">Four</option>
    <
    option value="e">Five</option>
    <
    option value="f">Sixpack</option>
    </
    select>
    <
    br />
    <
    input type="submit" name="doit" value="Submit" />
    </
    form
    It'S important that the "name" of the variablen within the <select>-tag end with '[]' so that an array can be created. The script 'result.php has this variable in $_REQUEST['numbers'] (or $HTTP_POST_VAR['numbers'] with PHP < 4.1.0) as array and the values of thgis array can be listed.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  3. #3
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make your select able to select multiple items and make it an array.

    <select name="my_list[]" size=20 multiple>
    ...
    </select>

    Then before submitting, select all elements of the list, either by hand or by using javascript. Then they will all be passed to the script in "my_list" array.
    Nasir
    nasir.us

  4. #4
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    frezno beat me to it LOL
    Nasir
    nasir.us

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, faster but much more typos in it. otherwise i wouldn't have made it
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both for your responses.

    Can you point me to a thread or tutorial which described how to make a javascript function or theory on how to write a function that will actively selected all options in a select list?

  7. #7
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function selectAllOptions(formName, selectElement)
    {
    	var selectObject = document.forms[formName].elements[selectElement];
    	var selectCount  = selectObject.length;
    	for (var i = 0; i < selectCount; i++) {
    		selectObject.options[i].selected = true;
    	}
    	return true;
    }
    Adaptation from a piece of javascript found in phpMyAdmin.
    Nasir
    nasir.us


Bookmarks

Posting Permissions

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