0

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>
3
  • 1
    You line is missing echo, try this: <?php echo $nyDeltaker->hentYrke() ?>, read more about echo here w3schools.com/php/php_echo_print.asp Commented Mar 9, 2018 at 19:54
  • The options don't exist within the element when the page loads, that's your problem. Commented Mar 9, 2018 at 19:57
  • Does your Javascript live in the same file as your HTML? Commented Mar 9, 2018 at 20:09

1 Answer 1

1

Since the options of the select element don't exist on the page at page load, you need to add one more line to essentially "re-set" the value of the select element after they've been added.

<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));
    }

    // this is the line to add
    selectBox.value = document.getElementById('yrke').getAttribute('value');

</script>

Demo using a hardcoded value of 'teacher'

Note that depending on whether or not you are actually echoing within your hentYrek() function, you may need to do as AamirR suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, while I forgot to add "echo" this time, I tried with it some of the other times. Your answer works perfect. Was trying something similiar with: selectBox.value = "<?php echo $nyDeltaker->hentYrke() ?>" But while I am still learning php and html I only came this far and was not quite sure why it wouldnt work. Thanks so much for the help!

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.