#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    51
    Rep Power
    3

    Help making a making a multi select imput form sticky


    Hi Everyone

    I have a form field that has a multi select imput field .

    i am now trying to make this imput box "sticky" i.e return all the values to the field that the user has just clicked.

    i have drafted the code for it and the form seems to work perfectly ; the values are actaully all received. the only problem is that the imput field is not showing the values that have previouly selected.


    i enclose my code below and would really appriciate any advice/assistance on this;


    PHP Code:
      <?php  
    //initilize the values
        
    $source4='';   $fleunt_lang=''$LANGUANAGE_pref=''
                                  
            
    //return the values from the select box 
                  
    if(isset($_POST['fleunt_lang']))
                          {          
                    
    $lang_pref $_POST['fleunt_lang'];
                    foreach (
    $lang_pref as $country
                        {
    $source1 .= $country.", ";}
                        
    $language_pref substr($source10, -2);
                    
                      
    $LANGUANAGE_pref   mysqli_real_escape_string($dbc,trim($language_pref  ));
                    
                          }

    //place the values into an array 
              
                         
    $arr1 = array(    $LANGUANAGE_pref );
                      
    ?>
        <?php  
      THE IMPUT BOX
      
    //lang_list() is taken from a database of languages-plus their Id

        //i will later attempt to check if the $langauge_id is in the Array-the array was previouly supplied from the returned values above. 
        
      
    echo'       <form action= ""  method="post" name="form_jobsort"    id="form_jobsort "       >
           
                       
                
         
        
             <select name="fleunt_lang[]" id ="fleunt_lang"      multiple="multiple"       size="6"       style="font-family: Tahoma; width: 300px;     font-size: 12pt" >'
    ;
              
        
           
          
    $language_list      =    language_list( );
         
        while  (
    $row mysqli_fetch_array $language_listMYSQLI_ASSOC)) 
            
        {     
            
            
            
    $language_id =   safe_output$row['language_id']) ;
            
    $language  =   safe_output$row['language']) ;
            
            
                

           if (
    in_array($language_id,   $arr1))
                {
                      echo 
    '<option value="' $language_id  .'" selected>' .$language  '</option>';
                }
              else
                {
                echo 
    '<option value="' .$language_id  .'">' $language '</option>';
                }
                            
                
                
         
        } echo
    '</select>'';

    ?>

    </form>

    the form seems correctly coded and should be return "Selected" for values that are in the array. but it is not doing that.
    thank you so much for your help

    warm regards

    Andreea
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I have no idea what you're doing there.

    You take the values from $_POST['fleunt_lang'], put them into a comma-separated list (why?), then escape the whole string and put it into an array (as the only element).

    Then you apply mysqli_fetch_array() to an empty array (??) and check if the language_id from the non-existent query occurs in that one-element array you created earlier. What?

    Either you've forgotten half of your code (where's the query?), or this whole thing makes absolutely no sense. Just for your information: Putting a string like "1,2,3" into an array does not magically split the string into numbers and get you a three-element array. You just get an array with a string in it.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    51
    Rep Power
    3
    Originally Posted by Jacques1
    Hi,

    I have no idea what you're doing there.
    Hello Jacques1 and everyone else

    thank you for your answer.

    you asked why i put the returned values into a "comma-separated list". i did this beucase i wanted to place a comma between the values and then place them into an array.

    i have obviously not done this correctly; can u give me some advice as to how to place the returned values into an array.


    the reason why i want to place the values into an array is so that i can later use teh in_array() function to select the selcted values in my "select imput " filed.

    the mysql_fetch_array was indeed used to bring out values from the database. i did not include the function to bring out the values from the database but have now enclosed it:


    PHP Code:


     
    function  country_list (    )
             {
                 global 
    $dbc;
            
                
    $select "  SELECT  
                           country_id,
                          country  "
    ;
                             
                          
    $from   "    FROM
                countrylist  "
    ;
                
             
    $query $select.$from  ;
                 
    $result mysqli_query ($dbc$query);
             return 
    $result 
                      } 
    so in summary. i think the issue will be ; what is the correct way to place the returned values from the "select form field" into an array.

    warm regards

    Andreea
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Are you sure this is the right function? In your code, it's language_list(), language_id etc.



    Originally Posted by andreea115
    you asked why i put the returned values into a "comma-separated list". i did this beucase i wanted to place a comma between the values and then place them into an array.
    You seem to confuse two different things. An array is a "living" object of the program, it has nothing to do with commas or something. That's just how you describe the array in your code.

    What you're doing is kind of like writing the word "tree" on a piece of paper and thinking this is now an actual tree.

    You can append elements to an array with the "[]" syntax: $arr[] = ...

    But in this particular case, I'd simply use array_map() to trim the values of $_POST['fleunt_lang']:
    PHP Code:
    $selected_languages array_map('trim'$_POST['fleunt_lang']); 
    You also seem to be confused about when and where to use escaping functions.

    Escaping is done right before you put a value into an "executable" context like a database query or HTML markup. In any other case you need to use the original values, especially when you want to check them.

    What you're doing just garbles the input. You apply the SQL escaping method to the user input and the HTML escaping method to the database values. If this was about more complex strings than just language names, you would end up with complete nonsense: Assuming both value are
    Code:
    'a' < 'b'
    Then you'd end up comparing
    Code:
    &apos;a&apos; &lt; &apos;b&apos;
    with
    Code:
    \'a\' < \'b\'
    So just check the values from the database against the values from POST:
    PHP Code:
    $selected_languages array_map('trim'$_POST['fleunt_lang']);

    // not sure if that's the right query: country = language?
    $languages_query $dbc->query('
        SELECT   
            country_id, 
            , country
        FROM 
            countrylist
    '
    );
    while (
    $row $languages_query->fetch_row()) {
        list(
    $country_id$country) = $row;
        
    $language_is_selected in_array$country_id$selected_languages );
        
    // *now* you need to escape the values:
        
    echo '<option value="' safe_output($language_id)  . '"' . ($language_is_selected ' selected="selected"' '') . '>' safe_output($language) . '</option>';


IMN logo majestic logo threadwatch logo seochat tools logo