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.