Jump to content

dynamic drop-down list with unique option selected name


LivinInChina

Recommended Posts

Hi everyone!

 

I have a form which sets its values from a $_POST of itself or defaults values which I manually set.

What I'd like is to build the drop-down list dynamically. So for that, I want it to display in the first option

selected either a default value (here large) or the $_POST('fontSize') you'll see in the following code.

And if the $_POST('fontSize') or the defaukt value is selected in the first option tag so this value SHOULD NOT appear

again in the drop-down list. And that's where I block.

 

<form action="monform.php" method="post">
  <p>
    <label for="fontSize">Taille :</label>
    <select id="fontSize" name="fontSize">

	<?php

        $post_size = 25;

        $size_list = array(
                    20 => 'tiny', 
                    25 => 'small', 
                    30 => 'medium', 
                    40 => 'large'
                );

        foreach ($size_list as $size => $name) {
            if(intval($post_size) == $size){
                echo "<option value='".$size."' selected='selected'>".$name."</option>";
            }else{
                echo "<option value='".$size."'>".$name."</option>";
            }
        }

        ?>

    </select>
  </p>
</form>

 

Thganks a lot in advance for your help!

Link to comment
Share on other sites

you got some code wrong you are putting the $key where $value should be.

 

 

 


<?php

        $post_size = 25;

        $size_list = array(
                    20 => 'tiny', 
                    25 => 'small', 
                    30 => 'medium', 
                    40 => 'large'
                );

        foreach ($size_list as $size => $name) {
            if(intval($post_size) === $name){
                echo "<option value='".$name."' selected='selected'>".$name."</option>";
            }else{
                echo "<option value='".$name."'>".$name."</option>";
            }
        }

        ?>

Link to comment
Share on other sites

Thanks darkfreaks for you interest.

 

Though it still doesn't work but check this out, this code is cleaner but I still have the duplicate name in the list that I would like to dynamically get rid!  :)

 

<form action="test.php" method="post">
  <p>
    <label for="fontSize">Taille :</label>
    <select id="fontSize" name="fontSize">

	<?php

        $post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25);

        $size_list = array(
                    20 => 'tiny', 
                    25 => 'small', 
                    30 => 'medium', 
                    40 => 'large'
                );

        foreach ($size_list as $size => $name) {
            echo "<option value='".$size."'". ($post_size == $size? " selected='selected'" : "") .">".$name."</option>";
            }

        ?>

    </select>
  </p>
  <p>
    <input name="submit" type="submit" value="envoyer" />
  </p>
</form>

Link to comment
Share on other sites

oh ok, so I tried with your last code but it doesn't work, what do you think it comes from?

 

<form action="test.php" method="post">
  <p>
    <label for="fontSize">Taille :</label>
    <select id="fontSize" name="fontSize">

	<?php

        $post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25);

        $size_list = array(
                    20 => 'tiny', 
                    25 => 'small', 
                    30 => 'medium', 
                    40 => 'large'
                );

	foreach($size_list as $size => $name) {

		$selected = ($type= $post_size==$name) ? "selected='selected'" : "";

		echo "<option value='".$name."' ".$selected.">".$name."</option>";

	}

        ?>

    </select>
  </p>
  <p>
    <input name="submit" type="submit" value="envoyer" />
  </p>
</form>

 

Thanks a lot for your help!

Link to comment
Share on other sites

I use the dots and doublequotes only for a clearer view of my code because this list is only a part or my page so if I can have my variables highlighted it makes it easier to see 'em at a glance.

 

To get back on this code, not only it displays the duplicated selected name in the drop down, but also when I submit the form, the selected element is always large  :confused: I'm confused, this code is supposed to be easy but I'm relly having a big headache here. I might be tired  :-[

Link to comment
Share on other sites

changed your ternary  it was abit off.....

 

 


<?php

$post_size = isset($_POST['fontSize']) ? intval($_POST['fontSize']) : 25;

        $size_list = array(
                    20 => 'tiny', 
                    25 => 'small', 
                    30 => 'medium', 
                    40 => 'large'
                );
foreach($size_list as $size => $name) {
$selected = ($type= $post_size===$name) ? "selected='selected'" : "";	
echo "<option value='".$name."' ".$selected.">".$name."</option>";

}

        ?>

 

 

Link to comment
Share on other sites

Thanks now it's working properly when submitted.

 

However I start to wonder if this is possible to have this for instance:

 

<selected>  Large

                    Tiny

                    Small

                    Medium

 

 

INSTEAD OF THIS:

 

<selected>  Tiny

                    Tiny

                    Small

                    Medium

                    Large

 

No more duplicate! But I guess it's not possible, maybe using some js, but in that case I prefer not.

 

 

Link to comment
Share on other sites

There aren't necessarily any duplicates. It's the way dropdowns work.

 

The selected option is displayed in the "box". When you press the mouse button it drops down and displays all the options with the selected one highlighted in the list.

 

The way round is probably to write your own dropdown component, or modify with JS, so that it exhibits a different behaviour

Link to comment
Share on other sites

<form action="test.php" method="post">
<p>
<label for="fontSize">Taille :</label>
<select id="fontSize" name="fontSize">
<?php

$post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25);

$size_list = array(
20 => 'tiny',
25 => 'small',
30 => 'medium',
40 => 'large'
);

// output the selected/default option -
echo "<option value='$post_size' selected='selected'>{$size_list[$post_size]}</option>";
foreach($size_list as $size => $name){
// output the remainider of the non-selected options -
if($post_size !=$size){
	echo "<option value='$size'>$name</option>";
}
}

Link to comment
Share on other sites

Thanks a lot to all of you for your help.

 

As I already imagined and as Barand confirms, there is no way around, this is just the way the box is designed.

If I want to make it my way, js is my answer but right now I'm not interested in that solution so I'll keep it like that.

 

Again thanks to all of you  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.