0

I'm trying to select the value, of the dropdown called "Type", that's equal to the value of the PHP variable called "$Type" by using Javascript. I know there is a "selectindex" funciton in JS but "$Type" doesn't contain numbers only text.

I tried to use a loop I found on this site, but that didn't work for me.

PHP:

while(list($ArtikelID, $Type) = mysql_fetch_array($result))
{
  $arid = $ArtikelID;
  $te = $Type;
}

HTML + JS:

<form name="send" method="post" action="editartticlesdef.php">
    articlenumber: </br>
    <input readonly="readonly" name="ArticleID" value=<?php echo $arid ?>></br>
    Type: </br>
    <select name="Type" id="Type">
        <option value="Article">Article</option>
        <option value="Code">Code</option>
        <option value="News">News</option>
        <option value="Project">Project</option>
    </select></br>
<script>
    window.onload = function SelectType() 
    {
        var sel = document.getElementById('Type');
        var val = document.getElementById('<?php echo $te; ?>');
        for(var i = 0, j = sel.options.length; i < j; ++i) 
        {
            if(sel.options[i].innerHTML === val) 
            {
               sel.selectedIndex = i;
               break;
            }
        }
    }
</script>

</form>

I'm not using Ajax or Jquery, so that's why I tried to use the loop. But it doesn't work for some reason. The dropdown still selects the first option on default when loaded.

1
  • You don't need javascript to show value selected. You can write condition to set attribute selected Commented Oct 15, 2015 at 10:01

3 Answers 3

1

you can do it this way, using PHP.

<?php
$listArticle = array(
                'Article',
                'Code',
                'News',
                'Project'
            );
?>
<select name="Type" id="Type">
    <?php    
    foreach($listArticle as $article)
    {
        $selected = '';
        if($article == $te)
        {
            $selected = 'selected="selected"';
        }

        echo '<option '.$selected.'>'.$article.'</option>';
    }
    ?>
</select></br>
Sign up to request clarification or add additional context in comments.

2 Comments

This works! I had to edit a little bit though, you forgot a ; and your if loop should been "$te" instead of "$Type". So I edited your answer but it worked. Thank you!
Yes, thanks for correcting it. I just approved your modifications.
0

The index referred to in selectedIndex for a select menu is an integer referring to the option item index in the menu - not the value contained therein.

For the javascript, try:

 if( sel.options[ sel.options.selectedIndex ].value === val ){
      sel.selectedIndex = i;
      break;
 } 

1 Comment

Doesn't work either, it still will show the very first option of the dropdown when it loads.
0

You don't need the for loop at all, instead do (based on this):

window.onload = function SelectType() 
    {
        var sel = document.getElementById('Type');
        var val = document.getElementById('<?php echo $te; ?>');
        sel.value = val;
    }

This require the val to be presented in the options.

2 Comments

I tried your code, it causes my dropdown to become blank.
Well, as I said, the options must have one value the same as val. otherwise it would cause the select to become blank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.