我需要将free_text_55的值附加到一个输入框,该输入框具有相同的id,但是在第二秒的第一个值中,输入框的id相同

时间:2014-06-29 17:02:07

标签: javascript php

以数组的形式提取数据。

e.g

Array
(
    [0] => Array
        (
            [free_text_55] => free text to add on product 11111.
        )
    [1] => Array
        (
            [free_text_55] => free text to add on product 222222222.
        )
    [2] => Array
        (
            [free_text_55] => free text to add on product 3333333333333.
        )
    [3] => Array
        (
            [free_text_55] => free text to add on product 4444444444444444.
        )
)

我需要将free_text_55的值附加到具有相同ID的输入框。我数数组并开始循环。但它只将第一个值插入输入框。

echo "<script type='text/javascript'>document.getElementById('free_text55').value ='".$advert_edit_form_data[0][$j]['free_text_55']."';</script>";

1 个答案:

答案 0 :(得分:0)

连接PHP中的所有值,然后将其存储在输入框中:

$free_text55 = ''
foreach ($advert_edit_form_data[0] AS $a) {
    $free_text55 .= $a['free_text_55'];
}
echo "<script type='text/javascript'>document.getElementById('free_text55').value =".json_encode($free_text55.";</script>";

在将PHP变量转换为Javascript时最好使用json_encode(),以防有特殊字符需要编码。