将mysql数据提取到javascript数组中

时间:2014-01-02 03:28:27

标签: javascript php mysql arrays

我试图获取存储在javascript数组中的mysql数据,我想通过选择的选项将其自动填充到textarea中,但它不起作用,textarea框不显示任何内容,是否有任何解决方法?

<script type="text/javascript">
// Pre populated array of data
var myData = new Array();
<?php
$qry1 = mysql_query("SELECT * FROM `template`") or die(mysql_error());
while($rows = mysql_fetch_array($qry1)){
?>
    myData[<?php echo $rows['preset_id'] ?>] = "<?php echo $rows['contents'] ?>";
<?php
}
?>
//myData[1] = 'Some text'; <- this working
//myData[2] = 'Some other text';  <- this working
</script>

这里是代码插入textarea:

<form id="example" name="example">
<select id="selector" name="selector">
    <option value="" selected></option>
    <?php
    $qry = mysql_query("SELECT * FROM `preset`") or die(mysql_error());
    while($rows = mysql_fetch_array($qry)){
        echo '<option value="'.$rows['preset_id'].'">'.$rows['preset_subj'].'</option>';
    }
    ?>
</select>
<br />
<textarea id="populateme" name="populateme" />
</textarea>
</form>

<script type="text/javascript">
document.example.selector.onchange = updateText;

function updateText() {
    var obj_sel = document.example.selector;
    document.example.populateme.value = myData[obj_sel.value];
}
</script>

0 个答案:

没有答案