将 php 数组传递给 jquery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10758471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:58:21  来源:igfitidea点击:

pass php array to jquery function

phpjqueryarrays

提问by Albert Prats

I got a page with a form to fill it with custormers info. I got previous custormers data stored in a php array. With a dropdownlist users can fill the form with my stored data.

我有一个页面,上面有一个表格,可以填写客户信息。我将以前的客户数据存储在一个 php 数组中。通过下拉列表,用户可以使用我存储的数据填写表单。

what i have is a jquery function that triggers when the value changes and inside that function i whould like to update the form values with my stored data (in my php array).

我所拥有的是一个 jquery 函数,它在值更改时触发,在该函数中,我想用我存储的数据(在我的 php 数组中)更新表单值。

Problem is that i dont know how to pass my php array to the jquery function, any idea ??

问题是我不知道如何将我的 php 数组传递给 jquery 函数,知道吗?

this is how i fill the array:

这就是我填充数组的方式:

$contador_acompanantes = 0;

foreach ($acompanantes->Persona as $acomp) 
{
  $numero = $acomp->Numero;
  $apellidos = $acomp->Apellidos;
  $nombre = $acomp->Nombre;

  $ACOMPANANTES[$contador_acompanantes] = $ficha_acomp;
  $contador_acompanantes++; 
}

got my select object:

得到了我的选择对象:

<select name="acompanante_anterior" id="acompanante_anterior">
<option value="1" selected>Acompa?ante</option>
<option value="2">acompanante1</option>
<option value="2">acompanante2</option>
</select>

and this is my code for the jquery function (it is in the same .php page)

这是我的 jquery 函数代码(它在同一个 .php 页面中)

<script type="text/javascript">
$(document).ready(function() 
{ 
$('#acompanante_anterior').on('change', function() 
    {

    });
});
</script>

回答by mgraph

var arrayFromPHP = <?php echo json_encode($phpArray); ?>;
$.each(arrayFromPHP, function (i, elem) {
    // do your stuff
});

回答by imsky

You'll likely want to use json_encode, embed the JSON in the page, and parse it with JSON-js. Using this method, you should be aware of escaping </script>, quotes, and other entities. Also, standard security concerns apply. Demo here: http://jsfiddle.net/imsky/fjEgj/

您可能想要使用json_encode,将 JSON 嵌入到页面中,并使用JSON-js解析它。使用此方法,您应该了解转义</script>、引号和其他实体。此外,标准的安全问题也适用。演示在这里:http: //jsfiddle.net/imsky/fjEgj/

HTML:

HTML:

<select><option>---</option><option>Hello</option><option>World</option></select>

<script type="text/javascript">
    var encoded_json_from_php = '{"Hello":[1,2,3], "World":[4,5,6]}';
    var json = JSON.parse(encoded_json_from_php);
</script>

jQuery:

jQuery:

$(function() {
    $("select").change(function() {
        $(this).unbind("change");
        var val = json[$(this).val()];
        var select = $(this);
        $(this).empty();
        $.each(val, function(i, v) {
            select.append("<option>" + v + "</option>");
        });
    });
});

回答by Michael Roewin Tan

try this one..

试试这个..

<script type="text/javascript">
    $(document).ready(function() 
    { 
    $('#acompanante_anterior').on('change', function() 
        {
            my_array = new Array();

            <?php foreach($array as $key->val)?>
                my_array['<?php echo $key?>'] = '<?php echo $val;?>';
            <?php endif; ?>

        });
    });
    </script>