0
case '1':
document.getElementById(q15).options.length = 0;
for (i = 0; i < australia.length; i++) {
     createOption(document.getElementById(q15), australia[i], australia[i]);
     }
break;

The above code calls the array information:

function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }

The above code creates a drop down as below:

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

etc

What I need is to also get it to add some PHP script as well, is this possible? It would look like this (NOTE: ==1 the '1' needs to be a variable, could it be the '[i] from the top Javascript code?:

<option value="1" <?php if ($results['q14']==1) echo "selected";?>>1</option>
<option value="2" <?php if ($results['q14']==2) echo "selected";?>>2</option>
<option value="3" <?php if ($results['q14']==3) echo "selected";?>>3</option>
1
  • Just pass the selected value from PHP to JavaScript (e.g. json_encode()'ed values) Commented Aug 16, 2012 at 7:02

1 Answer 1

1

Because in a synchronous connection JavaScript is interpreted after PHP has already been parsed, You either can add php code to the JavaScript itself, or create an asynchronous AJAX request to a php script in order to check which option should be selected. For the first choice:

function createOption(ddl, text, value, selected) {
    var opt = document.createElement('option');
    opt.value = value;
    opt.text = text;
    opt.selected = selected;
    ddl.options.add(opt);
}

and in the call

createOption(ddl, "some option", "someopt", <?=($results['q14']==1)?'true':'false'?>);

Note that this JavaScript has to be parsed by PHP, so either include it in the *.php file or add *.js to the webservers handler mappings.

5
  • Hi Andre, thanks for the reply - that is almost what I'm looking for, see the above. The ==1 needs to be a variable and not set in the code? Commented Aug 16, 2012 at 9:25
  • Yes, it can be a $results['id']==$selected_Id Commented Aug 16, 2012 at 9:28
  • @BurakTAMTURK - thanks for the reply - could you elaborate, it's me but I can't quite figure out what you mean? Commented Aug 16, 2012 at 9:39
  • If you create createOption script in php, and if you want new created content will be selected, then you can selected true in the code. With following code. Commented Aug 16, 2012 at 9:47
  • If You know what the "australia" array looks like at the time when PHP generates the page, You could just generate (in the PHP script) calls to "createOption" for every entry. If You don't, please specify what You are trying to do. Commented Aug 16, 2012 at 17:54

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.