Hi guys
Im trying to pass a PHP array to JavaScript but i am unable to figure out how.
The array is created from a MySQL query:
Code:
$sql="SELECT * FROM tblEmployees WHERE employeeID = '".$q."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
I have no problem passing variables and other arrays to Javascript, but passing $row doesnt work.
Code:
$str = $row['firstName'];
echo $str;
$str displays the value of $row['firstName'] correctly.
But it doesn't pass the value to JavaScript, it just shows up blank:
This code gets the value of $str and tries to display it in a text box called txtTitle.
Code:
<script type="text/javascript">
var val = <?php echo json_encode($str); ?>;
document.getElementById("txtTitle").value = val;
</script>
Im pulling my hair out with this. I have searching through forums and i havent found any help.
Thanks in advance