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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Generating Array from SELECT options

    Hi Guys,
    I'm working and I need to some help.
    So I've written a PHP script that generates a SELECT input in my form. The HTML output is this:
    Code:
    <select id="users" name="users" style="width: 100%">
    <option value="35">35</option>
    </select>
    The php code that does this is:
    PHP Code:
    <? php
    //Generates a select input based on the given paramters tablename, columnname,
    function GenerateSelect($table$column$style$controlname$valuecolumn) {
       
    mysql_connect('localhost''db''password');
       
    mysql_select_db('db');
       try {

                    
    // Now, I'm selecting both columns from the table, so it's all good.
          
    $sql "SELECT $column,$valuecolumn  FROM $table";
          
    $result mysql_query($sql);

          
    $style "'" $style "'";
          echo 
    "<select id='$controlname' name='$controlname' style=$style>";
          while (
    $row mysql_fetch_array($result)) {
             echo 
    "<option value='" $row[$valuecolumn]  . "'>" $row[$column] . "</option>";
          }
       } catch (
    Exception $e) {
          echo 
    'Caught exception: '$e -> getMessage(), "\n";
       }
       echo 
    "</select>";
    }
    ?>
    and from my form I simply do:
    PHP Code:
    <?php
    GenerateSelect
    ('table''FirstName''width: 100%''users''Age');
    ?>
    till here everything looks good. after this, the user can interact with the page, can select user and add them to a "list". With JavaScript I do like this:
    Code:
    <script type="text/javascript">
    			//function to add the selected user to the list
    			function add_List() {
    				//Create variable for the corresponding user's select 
    				var elt = document.getElementById('users');
    				// read it's text and value
    				var usrnme = elt.options[elt.selectedIndex].text;
    				var usrid = elt.options[elt.selectedIndex].value;
    				// create variable of type option
    				var elOptNew = document.createElement('option');
    				// set it's text and value
    				elOptNew.text = usrnme;
    				elOptNew.value = usrid;
    				//Create variable for the corrosponding distribution list's select 
    				var elSel = document.getElementById('distribution_list');
    				//check if user is not already added, then add
    				if (! this.is_useradded(usrid)) {
    					try {
    						elSel.add(elOptNew, null);
    						// standards compliant; doesn't work in IE
    					} catch(ex) {
    						elSel.add(elOptNew);
    						// IE only
    					}
    				} else {
    					alert("Selected user already exists in the list, choose another one!");
    				}
    			}
    </script>
    so with this I can have such output:
    Code:
    <select id="list" multiple="" name="distribution_list" style="width: 100%">
    <option value="33">Glenn</option>
    <option value="35">Peter</option>
    <option value="27">Ali</option>
    </select>
    Here's what I need to do, I need to send this information via POST when submitting the form. I'm thinking about arrays. Do you think it is wise? I could also store the whole thing as a single string and send the whole thing in POST?
    suppose the string would be "33-Glenn/35-Peter/27- Ali/". I use the signs for later to decode. How do I send this string via POST? generate an array using JavaScript and send that array via POST?

    I appreciate any help in advance,

    cheers,

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    You've got the right idea. You declare a javascript array, then you push the data into it, then you make all the data the value for a hidden input for a form. If you send it up all as one block you will have to split it out in two phases, first the members listings, then the individual data. The way I've done it is to separate the main listings with the upright stroke '|', and the other entries are naturally separated by commas. PHP will not see the JS array as an array, so it has to be 'exploded' out. It will probably take a bit of fiddling to get it just right, but the process is pretty straightforward.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    Why would you add both the id and name to the list?

    After all, the ID is all you need to identify the person in the database, no?

    So if you just created a hidden field and plunked the "list" into it as a simple comma-delimited string of id numbers *only* it would make the PHP code when you submit the <form> trivial.

    That is, at the same point in your code where you add the <option>, also just do
    Code:
    document.getElementById("hiddenList").value += "," + usrid;
    If the hidden field starts out like this:
    Code:
    <input type="hidden" name="hiddenList" id="hiddenList" value="0" />
    Then, when the <form> is submitted, the PHP code can receive it as:
    Code:
    $list = $_POST["hiddenList"];
    And then, just for example, you could put all those users into a DB table thus:
    Code:
    $sql = "INSERT INTO selectedUsers (id,name) " 
         . " SELECT usrid, usrname FROM allUsers " 
         . " WHERE usrid in (" . $list . ")";
    And presto! With one SQL statement you have copied all the relevant info into the selectedUsers (or whatever name you use) table.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    Oh, and note that if nobody is added to the list, the hidden form field will still have a value of "0" and so, if you don't have any usrid with a value of 0, nothing gets added to the selectedUsers table but there is no error!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your replies. See, I have the following HTML:
    Code:
    <select id="users" name="users"><option value="27">Ali</option><option value="27">Ali</option><option value="30">Phil</option><option value="19">Jassem</option><option value="69">PHP User</option><option value="0"></option><option value="69">PHP User</option><option value="0"></option><option value="69">PHP User</option><option value="0"></option><option value="69">PHP User</option><option value="123">X</option></select>
    I will let the user to select a user from the list to add it to the list, when clicked on add button, the name will be added to the following multiple select to display for the user:
    Code:
    <select id="distribution_list" multiple="" name="distribution_list">
    					<option value="27">Ali</option><option value="19">Jassem</option></select>
    this maeas, the user has added two users.
    Now when the user submits the form, I need to have a list of the selected users. Something as a string would be enough. I need to send this string to a PHP processor via POST. The string could look like:
    name1-id1|name2-id2 and so on ... remember the fist SELECT option, it had user's name as title and users's id as value. How do you think I can achieve this? I was thinking that I may have to create a hidden form field, but then how would create a string?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    You don't need JavaScript for this, at all.

    Just change the name of the <select> to match PHP's silly requirement for multiple-value names and do it all in the <option> values:
    Code:
    <select multiple name="distribution_list[]">
        <option value="27-All">Ali</option>
        <option value="19-Jassem">Jassem</option>
        <option value="33-Mulan">Mulan</option>
    </select>
    Then, in your PHP code, you can do:
    Code:
    $dlist = $_POST["distribution_list"];
    for ( $i = 0; $i < count($dlist); ++$i )
    {
        $idname = explode( "-", $dlist[$i] );
        $id = $idname[0];
        $name = $idname[1];
        ... do something with each id and name ...
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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