Add phparray variable inside javascript dynamically

I am using an impromptu jquery plugin. I have options to insert html under the "html" option, but I need to populate an array of values ​​that I get from php. I can’t insert it.

I want to populate the select box with the values ​​that are in the php variable. I tried:

JSFIDDLE

var statesdemo = {
    state0: {
        title: 'Terms of Use',
        html:'<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td><strong>Data1</strong></td><td>:</td><td id=\'Data1\'>   <select name=\'Data1\' id=\'cart_wonid\' class=\'Data1\'><?php echo $options;?> </select></td></tr></table>',
        buttons: { Cancel: false, Agree: true },
        focus: 1,
        submit:function(e,v,m,f){
            if(v){
                e.preventDefault();
                $.prompt.goToState('state1', true);
                return false;
            }
            $.prompt.close();
        }
    },

Update 1:

1- The main idea is a drop-down list inside the popup.
2- I want to get the dropdown data from the mysql query that I wrote on the server side (php). Thus, without this popup, the @tomloprod idea suggested working fine. Now returning to the popup, I can add html content like

 html : '< table > < /table > '  

But I want to insert a php variable inside it, like

html : '< table > < ?php $myvariable ?> < /table >'
+4
2

... ... "json_encode ($ options)" ... , stackoverflow @tomloprod Juan Mendes

+2

1: json_encode ()

php javascript :

var javascript_Array = <?php json_encode($php_Array); ?>;

2: php array select

PHP; JS...

<select name="data1" id="cart_wonid" class="Data1">
  <?php
     $len = count($options);
     for($c=0;$c<$len;$c++){
        echo '<option value="'.$options[$c].'">$options[$c]</option>';
     }
  ?>
</select>

3: javascript array select

HTML:

<select name="data1" id="cart_wonid" class="Data1"> </select>

JavaScript:

// Define the array
var javascript_Array = []; 

// Push elements
javascript_Array.push("Hi");
javascript_Array.push("Bye");

// Get the select with id cart_woneid
var sel = document.getElementById('cart_wonid');

// Fill the "select" with the elements of array
var i, len = javascript_Array.length;

for(i = 0; i < len; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = javascript_Array[i];
    opt.value = javascript_Array[i];
    sel.appendChild(opt);
}

JSFiddle: http://jsfiddle.net/1dpud00v/

+4

Source: https://habr.com/ru/post/1568891/


All Articles