I am trying to built a JS String array from a PHP String array, but my JS skills are a bit limited. I would like also display and return a selected JS row to a HTML or PHP string. Is it possible?
I would appreciate any help.
PHP part (working):
$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 part1:
<script type="text/javascript">
var myArray=new Array();
function buildarray(id, text){
myArray[id]=text;
}
</script>
Javascript part2:
<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>
Thank you in advance.
echo "<script language=javascript>buildarray($i,$data)</script>";
is wrong. Should beecho "<script language=\"javascript\">buildarray($i,$data)</script>";
Note howlanguage="javascript"
needs to be escaped. – luastoned Dec 14 '11 at 13:19language
is deprecated since 1999. – Saxoier Dec 14 '11 at 13:59