-1

I have been trying to get PHP to populate HTML multi-select form with pre-selected values based on previous saves done by the user. I found helpful examples here and elsewhere that helped conceptually understand what needs to be done but I cannot get the syntax to work.

Here is the relevant code snippet

<?php
             // This array will hold all options to be displayed in multi-select form
            $allservices = array('amazon','facebook','twitter','reddit');

             // $blockedservices is variable comes as a string from database like amazon,facebook.
             // Initially the value of the array is NULL for new users or user that never submitted.
             // I am exploding here as array to make it easier to work with like ['amazon','facebook']
            $selectedservices = explode(",", $blockedservices);

            ?><select name="services[]" multiple="multiple"><?

            //looping through all options
            foreach($allservices as $option){   
               //trying to see if option was preselected 
               if($selectedservices == $option) { 
               ?><option selected="selected" value="<?php echo $option;?>"><?php echo $option;?></option><?
               }
              else {
              ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?
              }
           }
        ?></select>

Currently, this results in Undefined variable: option in...

Update

Based on the feedback, using <?php instead of just <? resolves the undefined error issue. However, the pre-selected options are not displaying. Rather, all options are displayed with no pre-selection even when pre-selection exists.

I also tried in_array with the same result of not identifying the pre-selected option.

Here is another snippet with in_array:

<?php
            $allservices = array('amazon','facebook','twitter','reddit');
            $selectedservices = explode(",", $blockedservices);

            ?><select name="services[]" multiple="multiple"><?php
            foreach($allservices as $option){
            if(!empty($selectedservices)){

            if(in_array($option,$selectedservices)){
              ?><option selected="selected" value="<?php echo $option;?>"><?php echo $option;?></option><?php
            }
            else {
              ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?php
            }

           }else{
           ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?php
        }
        }

        ?></select>

Update 2

I posted an answer below that addresses all the code and syntax issues. The answer posted works as intended in all browsers. Hope it helps someone needing to accomplish the same thing in the future. Thanks to everyone for the comments that helped along the way.

bnks
  • 11
  • 3
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – gp_sflover Apr 17 '21 at 13:42
  • 1
    `$selectedservices` is an array, you should use `in_array()` to check for presence – brombeer Apr 17 '21 at 13:42
  • Do you have `short_tags` enabled? (short_open_tag php.ini) – brombeer Apr 17 '21 at 13:46
  • @gp_sflover Thank you for the reference. I did read about the error but I am not sure how to resolve it in the context of what I am trying to do. That's really what I need help with. – bnks Apr 17 '21 at 13:47
  • @brombeer Thank you for the recommendation. I did try that last night but it resulted in a different error because `$selectedservices` can be NULL which `in_array()` had an issue with. – bnks Apr 17 '21 at 13:49
  • 1
    Then make sure to use `in_array` only when it's not null – brombeer Apr 17 '21 at 13:51
  • @brombeer I did try `in_array` when not empty. No syntax errors but pre-selected options still do not display. I provided update code in original question. Thanks. – bnks Apr 17 '21 at 14:50
  • Hm, cannot reproduce, testing this shows blocked items as selected. What does `$blockedservices` look like? Make sure it doesn't contain spaces `$blockedservices = 'facebook,amazon';` https://3v4l.org/Ku1ij – brombeer Apr 17 '21 at 15:29
  • @brombeer it appears the `selected` value within the ` – bnks Apr 17 '21 at 15:46

2 Answers2

0

Just replace the <? to <?php and this error will disappear because you disable the short tag option in your php.ini file Update

<?php
            $allservices = array('amazon','facebook','twitter','reddit');

            $blockedservices ="amazon,facebook";
            $selectedservices = explode(",", $blockedservices);

            ?><select name="services[]" multiple="multiple"><?php

            //looping through all options
            foreach($allservices as $option){   
               //trying to see if option was preselected 
               if(in_array($option, $selectedservices)) { 
               ?><option selected="selected" value="<?php echo $option;?>"><?php echo $option;?></option><?php
               }
              else {
              ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?php
              }
           }
        ?></select>

there's another solution go to C/php/php.ini file and search short_open_tag by default it'll be Off change it to short_open_tag=On

enter image description here

  • Thank you. That does address the `Undefined` error but I still cannot get the pre-selected option to display. I updated the question with the changes. – bnks Apr 17 '21 at 14:49
  • I update the answer please look at it, I used the same code and used the in_array method and it's worked, please if the answer helpful mark my answer as helpful, so anyone who will have the same question will go to the helpful answer. – Abdlrahman Saber Apr 17 '21 at 16:24
0

Thanks to everyone for all the help with this. Based on all the comments and responses, there were three issues with the original code:

  • I should have used <?php as opposed to <?
  • I should have used in_array
  • The HTML syntax for selected value in <option> tag was out of place

This updated code fully resolves all the issues and works as intended:

<?php
            $allservices = array('amazon','facebook','twitter','reddit');
            $selectedservices = explode(",", $blockedservices);

            ?><select name="services[]" multiple="multiple"><?php
        foreach($allservices as $option){
            if(!empty($selectedservices)){

            if(in_array($option,$selectedservices)){
              ?><option value="<?php echo $option;?>" selected><?php echo $option;?></option><?php
            }
            else {
              ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?php
            }

           }else{
          ?><option value="<?php echo $option;?>"><?php echo $option;?></option><?php
        }
        }

        ?></select>
bnks
  • 11
  • 3