I am currently trying to set the value of an option in a select box from a php variable.
Just like a log in screen saves your username if you type the wrong password, I want to save the value of the option in the scroll box if something went wrong with submitting.
The problem arises now that I am using a script with an array to fill in the options in the select box. (PS have alot more choices, but cut down for example)
<script>
var yrkeValg =
[
{
"text" : "Police",
"value" : "police"
},
{
"text" : "Teacher",
"value" : "teacher"
}
];
var selectBox = document.getElementById('yrke');
for(var i = 0, l = yrkeValg.length; i < l; i++)
{
var option = yrkeValg[i];
selectBox.options.add(new Option(option.text, option.value, option.selected));
}
</script>
I have tried to set the value from my php class function "hentYrke()", but it get's ignored. Have checked and the function value is correct. How can I for example set teacher as the value from a php variable?
<select id="yrke" name="yrke" value="<?php $nyDeltaker->hentYrke() ?>"></select>
echo, try this:<?php echo $nyDeltaker->hentYrke() ?>, read more about echo here w3schools.com/php/php_echo_print.asp