0

The following works well:

var options = '<select><option value ="Unknown">Unknown</option><option value ="Yes">Yes</option><option value ="No">No</option><option value ="Both">Both</option></select>';
alert(options)

However, if that string is produced via a PHP foreach, the variable options is not recognized, hence I cannot use with JS. For example:

<?php foreach($pickListFields as $field_id => $options): ?>
    <?php 
        $options_array = explode("\n", $options); 
        $options_select = '<select>';
        foreach($options_array as $k => $option) {
            $options_select .= '<option value ="' . $option . '">' . $option . '</option>';
        }
        $options_select .= '</select>';
    ?>
    var options = '<?= $options_select ?>';
<?php endforeach; ?>

The above variable options produced, does not work, even though when I see the source code with Firefox I can see that var options is:

var options = '<select><option value ="Unknown">Unknown</option><option value ="Yes">Yes</option><option value ="No">No</option><option value ="Both">Both</option></select>';

Why then it cannot be used, if it is the same as the first example? I cannot alert that, or assign it to a field, but I can with the first example.

5
  • Are you sure that the source code given is exactly the same? I would also try removing the space before the equality sign. Commented Jan 16, 2012 at 16:19
  • It is, you can see it... I just copy pasted. Commented Jan 16, 2012 at 16:20
  • Where exactly are you trying to use it? I take it you are doing this within a javascript block? Can we see the $options from $pickListFields? Commented Jan 16, 2012 at 16:28
  • You have the options, they are the ones that I put in the var options, Unknown, etc. Commented Jan 16, 2012 at 19:33
  • Is it possible there are JavaScript errors or warnings? This may halt processing of your script. Commented Jan 16, 2012 at 22:19

2 Answers 2

1

You are using php tags inside php instead of javascript tags:

var options = '<?= $options_select ?>';

should be:

echo '<script language="javascript" type="text/javascript">var options="' . $options_select . '";</script>';
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if this works... It is also adding that text in my page.
OK I had to remove the <script> part, since I was already inside javascript. But it is still not working, the new variable options obtained cannot be used :(
0

try maybe html encode all output options, there can be some special chars hidden somewhere

Comments

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.