having an option tag being 'selected'

it only select the first option

Page 1 of 1

12 Replies - 1731 Views - Last Post: 17 September 2010 - 12:48 PM Rate Topic: -----

#1 solerjim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-February 09

having an option tag being 'selected'

Posted 14 September 2010 - 10:21 PM

good day....

i'm having a problem printing the 'selected' property in the 'option' tag...

here's a snippet of my code:

            <tr>
                <td>To : </td>
                <td>&nbsp;
                <?php
                    $get_GE = "SELECT * FROM tbl_portal_user WHERE access_title = \"Faculty\" AND department = \"GE\"";
                    $result_GE = $database->query($get_GE);
                    echo "<br />&nbsp;General Education:<br />";
                ?>
                    &nbsp;<select style="width:200px;" name="faculty[]" size="4" multiple="multiple">
                        <?php
                        $to2=$to;
                            while($select_GE = $database->fetch_array($result_GE))
                            {
                                if(!isset($to)){
                                    echo "<option value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
                                }
                                else{
                                    $key = explode(",",$to);
                                    for($l=0;$l<=count($key);$l++){
                                        if(strcmp($key[$l],(string)$select_GE['id'])==0){
                                            echo "<option selected value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
                                            break;
                                        }
                                        else{
                                            echo "<option value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
                                            break;
                                        }
                                    }
                                }
                            }
                        ?>
                    </select>
                </td>
                <td>&nbsp;
                <?php
                    $get_IT = "SELECT * FROM tbl_portal_user WHERE access_title = \"Faculty\" AND department = \"IT\"";
                    $result_IT = $database->query($get_IT);
                    echo "<br />&nbsp;Information Technology:<br />";
                ?>
                    &nbsp;<select style="width:200px;" name="faculty[]" size="4" multiple="multiple">
                        <?php
                            while($select_IT = $database->fetch_array($result_IT))
                            {
                                if(!isset($to2)){
                                    echo "<option value={$select_IT['id']}>{$select_IT['first_name']} {$select_IT['last_name']}";
                                }
                                else{
                                    $key2 = explode(",",$to2);
                                    for($k=0;$k<=count($key2);$k++){
                                        if(strcmp($key2[$k],(string)$select_IT['id'])==0){
                                            echo "<option selected value={$select_IT['id']}>{$select_IT['first_name']} {$select_IT['last_name']}";
                                            break;
                                        }
                                        else{
                                            echo "<option value={$select_IT['id']}>{$select_IT['first_name']} {$select_IT['last_name']}";
                                            break;
                                        }
                                    }
                                }
                            }
                        ?>
                    </select>
                </td>
                </td>
            </tr>


what happens is when i saved the selected 2 different options, the values are saved in '$to', they are joined. when i tried to explode the string and turned it on an array, i think it does not enter the second 'select' tag.

thanks in advance for the help...

Is This A Good Question/Topic? 0
  • +

Replies To: having an option tag being 'selected'

#2 Chalie9809  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 51
  • Joined: 29-January 09

Re: having an option tag being 'selected'

Posted 14 September 2010 - 10:35 PM

Hi solerjim,

It seems that your haven't set the $to variable anywhere in what you provided, is it set previously in the script. If it is we will need that to help you debug.

Cheers
Charlie

This post has been edited by Chalie9809: 14 September 2010 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: having an option tag being 'selected'

Posted 14 September 2010 - 10:45 PM

To make an option selected, you have to do <option selected='selected'>. Just including the attribute 'selected' by itself won't do anything, and will actually be read by an HTML validator as invalid syntax (because it is).
Was This Post Helpful? 0
  • +
  • -

#4 solerjim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-February 09

Re: having an option tag being 'selected'

Posted 14 September 2010 - 11:15 PM

@ Chalie9809:

here's where i get the values from my database:
if(isset($_GET['discussion_id']))
    {
        //prepare values for editing
        
        $sql = "SELECT * FROM tbl_portal_faculty_updates
                WHERE id = {$_GET['discussion_id']}";
                
        $discussion = $database->fetch_array($database->query($sql));
        
        $title = $discussion['faculty_update_title'];
        $from = $discussion['faculty_update_author'];
        $to = $discussion['faculty_recipient'];
        $content = $discussion['faculty_update_content'];
        $type = $discussion['faculty_update_type'];
        
    }



@Valek
As far as i know, when you just include the selected property, it's default value is 'selected', so, it works even without doing
<option selected='selected'>

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: having an option tag being 'selected'

Posted 14 September 2010 - 11:23 PM

Have you tried it with manually specifying the value?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,963
  • Joined: 08-June 10

Re: having an option tag being 'selected'

Posted 14 September 2010 - 11:23 PM

View PostValek, on 15 September 2010 - 04:45 AM, said:

To make an option selected, you have to do <option selected='selected'>. Just including the attribute 'selected' by itself won't do anything, and will actually be read by an HTML validator as invalid syntax (because it is).

I think you are confusing HTML with XHTML. HTML is fine with attributes like select and multiple by themselves, but XHTML follows XML rules and as such needs attributes to have values.

P.S.
If you set the Content-Type as "text/html", as most XHTML websites are set (thanks to IE), rather than the proper XML Context-Type, no browser enforces the XML rules. Meaning, leaving out the attribute values causes no ill effect, even with the XHTML doctype set.

This post has been edited by Atli: 14 September 2010 - 11:27 PM

Was This Post Helpful? 2
  • +
  • -

#7 solerjim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-February 09

Re: having an option tag being 'selected'

Posted 14 September 2010 - 11:25 PM

@Valek
yes, i have tried it... and also tried putting the value 'selected'...

still, selects the first item in the first select tag...
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: having an option tag being 'selected'

Posted 14 September 2010 - 11:28 PM

View PostAtli, on 15 September 2010 - 02:23 AM, said:

View PostValek, on 15 September 2010 - 04:45 AM, said:

To make an option selected, you have to do <option selected='selected'>. Just including the attribute 'selected' by itself won't do anything, and will actually be read by an HTML validator as invalid syntax (because it is).

I think you are confusing HTML with XHTML. HTML is fine with attributes like select and multiple by themselves, but XHTML follows XML rules and as such needs attributes to have values.

P.S.
If you set the Content-Type as "text/html", as most XHTML websites are set (thanks to IE), rather than the proper XML Context-Type, no browser enforces the XML rules. Meaning, leaving out the attribute values causes no ill effect, even with the XHTML doctype set.


Yeah, you're right. I've used XHTML for so long now that it's sorta gone into overlap on me there.
Was This Post Helpful? 0
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: having an option tag being 'selected'

Posted 15 September 2010 - 06:28 AM

Hello,

I notice your not actually closing any of your option tags, that could be related to the problem as not closing tags can result in unpredictable behavior.
Was This Post Helpful? 1
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: having an option tag being 'selected'

Posted 15 September 2010 - 07:49 AM

Good catch, Jstall!
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: having an option tag being 'selected'

Posted 15 September 2010 - 08:11 AM

View PostJstall, on 15 September 2010 - 01:28 PM, said:

I notice your not actually closing any of your option tags, that could be related to the problem as not closing tags can result in unpredictable behavior.

the closing </option> tag is optional in HTML (unlike in XML)
Was This Post Helpful? 2
  • +
  • -

#12 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: having an option tag being 'selected'

Posted 15 September 2010 - 09:46 AM

@Dormilich Ahh, didn't know that. Strange, I seem to remember running into problems because of not closing option tags when dynamically populating a select box. I must have been mistaken.


@solerjim I notice that when you explode your string into an array you start a for loop to iterate through it but you specify break in your conditions that result in it only being run once :


if(!isset($to)){
	echo "<option value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
}
else{
	$key = explode(",",$to);
	for($l=0;$l<=count($key);$l++){
		if(strcmp($key[$l],(string)$select_GE['id'])==0){
			echo "<option selected value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
			break;
		}
		else{
			echo "<option value={$select_GE['id']}>{$select_GE['first_name']} {$select_GE['last_name']}";
			break;
		}
	}
}




That would mean that only the first value in the array would be compared and regardless of the result the for loop would break. Is this what you intended to do? Are all the values that you expect to be in your select box showing up?
Was This Post Helpful? 0
  • +
  • -

#13 solerjim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 02-February 09

Re: having an option tag being 'selected'

Posted 17 September 2010 - 12:48 PM

sorry for the late reply. we were busy on doing the public area for our site.

regarding the break, i haven't noticed that break. i was making my cup of coffee and my brother sat in front of our computer and i did not know what he had done.

i guess it's the break.

*edited for some typos

This post has been edited by solerjim: 17 September 2010 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1