Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2012, 01:24 AM   PM User | #1
Fishboy
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Fishboy is an unknown quantity at this point
json_encode array and innerHTML

I'm trying to rewrite some existing code to simplify it a bit and am running into a problem passing values. I'm pulling a record from a mySQL DB and putting it into an associative array which is then json_encoded so I can manipulate the values using javascript. The code I have below is a test example so I can get the method and syntax down before coding the whole page.

I have several places on my page where I need to display either values from the database or input values from a form that are used in more than one place.

The code below works fine if I comment out the "document.getElementById" statement. The array is created and the alert works, but I can't get the value from the array to display in the div.

I'm just learning how to do this. What am I missing?


Code:
<?php
mysql_connect("mydbip", "username", "password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT * FROM sometable WHERE id='1'");

$row = mysql_fetch_array($result, MYSQL_ASSOC);
$myarr = json_encode($row);
mysql_free_result($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script id="source" language="javascript" type="text/javascript">
var ar = new Object();
ar= <?php echo $myarr;?>;
if(ar["variable1"] > 0) 
{
alert(ar["variable1"]);
document.getElementById("ar1").innerHTML = ar["variable1"];
}
</script>
</head>
<body>
<div id="ar1">0</div>
</body>
</html>
Fishboy is offline   Reply With Quote
Old 02-14-2012, 06:27 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
at the point of execution, the element with the ID "ar1" does not exist (is not yet rendered). you need to delay the JavaScript execution until the DOM is ready (or the page has finished loading).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
Fishboy (02-14-2012)
Old 02-14-2012, 01:40 PM   PM User | #3
Fishboy
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Fishboy is an unknown quantity at this point
Nevermind...created a function and added onload. Duh.

This does however lead to a question: is there a better way to do this?

I'm creating a form to collect several input values, then using javascript to use those values in combination with values pulled from the database to do some calculations and display them on another part of the form page.
Fishboy is offline   Reply With Quote
Reply

Bookmarks

Tags
array, getelementbyid, javascript, json, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:28 PM.


Advertisement
Log in to turn off these ads.