SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting multiple values from form.

    It's probably a simple question.. however, I don't know how to solve the following problem.

    I have a form where the user can select one or multiple values from a list:
    PHP Code:
    print "<FORM METHOD='POST' ACTION='insert_med_ok.php'>";
    print 
    "<select name='FunctionID' multiple size='5'>";
        
            
    $sql     "SELECT * FROM function ORDER BY Naam";
            
    $result mysql_query($sql);
            while (
    $row mysql_fetch_array($result)) {
                   
    extract($row);
                   
    $FunctionID $FunctionID;
                   print 
    "<option value='$FunctionID'>$Name</option>";
            }
    print  
    " </select><INPUT TYPE=SUBMIT value='add form'></form>"
    This form is submitted and passed on to the page where the info is submitted into the database.
    At this particular page, how can I read out the different values passed on by 'FunctionID'?

    I want to seperate them with a comma... so basically what I want to insert into the database is something like:
    1,4,5,38

    Any help would be appreciated.

  2. #2
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've changed the above form-action into GET... so variable FunctionID is passed on as many times as it is selected....

    But how do I print them into a variable and seperate them with a comma... i.e. '1,4,5,38'

    Please help...

  3. #3
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hoi jazztie,
    In your form you are able say your functionID is an array(people can choose more than one option from select)
    to do that change your 2nd line with
    PHP Code:
    print "<select name='FunctionID[]' multiple size='5'>"
    Then when you submit form (I recommend using post method whenever possible) u can reach $FunctionID array
    you said you wanna place commas between option values.
    PHP Code:
    $seperator ",";
    $NewVarimplode($seperator$functionID); 

  4. #4
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I print all of the variables in the Array FunctionID seperately?

    Say that the submit form posted '1,3,5'... and I want to use these ID's to look up the names of the function in the table Function using the ID's?

    I still don't understand how to use the individual values in the array $FunctionID.

    by the way, when I use extract, implode, explode on the $Function, in the way you suggested the parser returns an error: Wrong datatype in call to extract

    this is what I tried:
    - $newVar = implode(",",$FunctieID);

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well like sylow said by changing the name of the select list from FunctionID to FunctionID[] will make it an array that can hold mulitple values. Once the fomr is posted to your script yo ucan use this to access individual elements of the array.

    PHP Code:
    foreach($FunctionID as $key => $val) {
    print 
    "$val<br>";

    Also as a side note single quotes in HTML is poor coding.

    This line

    PHP Code:
    print "<FORM METHOD='POST' ACTION='insert_med_ok.php'>"
    Can be easily modified to be more efficient and comforming to xhtml and w3c standards by changing it to

    PHP Code:
    print '<FORM METHOD="POST" ACTION="insert_med_ok.php">'
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both!

    I will try it first thing tomorrow...



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
  •