【发布时间】:2011-12-14 12:09:45
【问题描述】:
我正在尝试从 PHP 字符串数组构建一个 JS 字符串数组,但我的 JS 技能有点有限。 我还想将选定的 JS 行显示并返回到 HTML 或 PHP 字符串。有可能吗?
我将不胜感激。
PHP 部分(工作):
$i = 0;
while($row = mysql_fetch_array($result)){ //get data from a mysql table.
$data = $row["sql_row"]; // string built
echo "<script language=javascript>buildarray($i,$data)</script>"; //call the JS function
$i = $i +1;
}
Javascript 第 1 部分:
<script type="text/javascript">
var myArray=new Array();
function buildarray(id, text){
myArray[id]=text;
}
</script>
Javascript 第 2 部分:
<script type="text/javascript">
function displayreturn(id){
document.write(myArray[id]); //dpisplay the data in the row number "id"
return myArray[id]; //return the string
}
</script>
提前谢谢你。
【问题讨论】:
-
您的
echo "<script language=javascript>buildarray($i,$data)</script>";错误。应该是echo "<script language=\"javascript\">buildarray($i,$data)</script>";注意language="javascript"需要如何转义。 -
@luastoned:
language自 1999 年以来已弃用。 -
是的,但是他想用的东西都需要正确转义,不过还是谢谢指出!
标签: php javascript html arrays