/    Sign up×
Community /Pin to ProfileBookmark

Right now I have a function that works with a static array that is defined inside the function:

[code]
function put_picker(){
var vals = [1,2,3,4,5,6,7,8,
9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24,
25,26,27,28,29,30,33,40,41,
44,46,51,54,55,66,68,69,
73,77,83,87,88,91,93,97,];
document.write(‘<div id=”container” class=”picker_container” style=”width:239px”>’);
for (ev in vals){
document.write(‘<div class=box onclick=”s(this)”>’+vals[ev]+'</div>rn ‘);
}
document.write(‘</div>’);
}
[/code]

All this does is use css to make a table of numbers that are clickable (basically a number pad). I would like to be able to make it so the array is created in my php function and then passed to the javascript function for output. Here is what I have tried so far with no luck.

[code]
function put_picker(vals){

document.write(‘<div id=”container” class=”picker_container” style=”width:239px”>’);
for (ev in vals){
document.write(‘<div class=box onclick=”s(this)”>’+vals[ev]+'</div>rn ‘);
}
document.write(‘</div>’);
}

<?php
echo “<script type=”text/javascript”>
var vals = new array(“.join(‘,’ , $vals).”);

put_picker(vals);
</script>
[/code]

And then I tried put the JS right in the code but still no luck:

[code]
<?php
echo “<script type=”text/javascript”>
var vals = new array(“.join(‘,’ , $vals).”);

document.write(‘<div id=”container” class=”picker_container” style=”width:239px”>’);
for (ev in vals){
document.write(‘<div class=box onclick=”s(this)”>’+vals[ev]+'</div>rn ‘);
}
document.write(‘</div>’);
</script>”;
?>
[/code]

I have looked at the source html that is created from the above code and the array seems to be created fine, it’s just not passing it properly to the javascript. Any help would be great. Thanks

Nick

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 02.2008 — Does the generated code from the 3rd code block look the same as the code in the 1st block?
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — This is what the source looks like from the 1st block:

[CODE]
<tr>
<td>
<script type="text/javascript">
put_picker();

</script>

</td>
</tr>
[/CODE]


Here is what I get from the 3rd block:

[CODE]
<tr>
<td><script type="text/javascript">
var vals = new array(1,2,4,3,5,6,7,13,19,22);

document.write('<div id="container" class="picker_container" style="width:239px">');
for (ev in vals){
document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>
');
}
document.write('</div>');
</script> </td>
</tr>
[/CODE]


So if you look at the function it does appear to be the same.
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — The only difference I can see is that in the code the array is defined like:

var vals = new array(1,2,4,3,5,6,7,13,19,22);

And in the function it is defined like:

var vals = [1,2,3,4,5,6,7,8,

9,10,11,12,13,14,15,16,

17,18,19,20,21,22,23,24,

25,26,27,28,29,30,33,40,41,

44,46,51,54,55,66,68,69,

73,77,83,87,88,91,93,97,];

Are those the same thing? Sorry pretty new to JS. Thanks for the help.
Copy linkTweet thisAlerts:
@FangOct 02.2008 — The extra comma (...,97[B][COLOR="Red"],[/COLOR][/B]]? at the end will give you an extra array value in IE

The second [I]document.write[/I] is incomplete.

Why write the table of numbers using JavaScript and not server side?
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — Sorry I guess I didn't outline my problem weel enough. My problem is that when I use the function put_picker() I see this first attached image(function.jpg). And when I use this code I see nothing on the page.

[CODE]
<?php
$vals = array(1, 2, 4, 3, 5, 6, 7, 13, 19, 22);

echo "<script type="text/javascript">
var vals = new array(".join(',' , $vals).");

document.write('<div id="container" class="picker_container" style="width:239px">');
for (ev in vals){
document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>rn ');
}
document.write('</div>');
</script>";
?>
[/CODE]


See the image code.jpg

[upl-file uuid=0284a7bc-ecaa-46cc-b8b2-3838aca23670 size=77kB]function.jpg[/upl-file]

[upl-file uuid=714a520c-b160-4129-a3bf-ec64a02b6fef size=47kB]code.jpg[/upl-file]
Copy linkTweet thisAlerts:
@FangOct 02.2008 — var vals = new [B][COLOR=Blue]A[/COLOR][/B]rray(".join(',' , $vals).");
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — Nope still nothing.

[CODE]
<?php
echo "<script type="text/javascript">
var vals = new Array(".join(',' , $vals).");

document.write('<div id="container" class="picker_container" style="width:239px">');
for (ev in vals){
document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>rn ');
}
document.write('</div>');
</script>";
?>
[/CODE]
Copy linkTweet thisAlerts:
@FangOct 02.2008 — This outputs the array:&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Basic HTML&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;

&lt;/script&gt;

&lt;style type="text/css"&gt;

&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;?php
$vals = array(1, 2, 4, 3, 5, 6, 7, 13, 19, 22);

<i> </i> echo "&lt;script type="text/javascript"&gt;
<i> </i> var vals = new Array(".join(',' , $vals).");

<i> </i> document.write('&lt;div id="container" class="picker_container" style="width:239px"&gt;');
<i> </i> for (ev in vals){
<i> </i> document.write('&lt;div class=box onclick="s(this)"&gt;'+vals[ev]+'&lt;/div&gt; ');
<i> </i> }
<i> </i> document.write('&lt;/div&gt;');
<i> </i> &lt;/script&gt;";
<i> </i> ?&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — Ok perfect, I got that to work. Now is there a way I can use the function and pass the array to the function. Is there something special about JS functions that I don't know? Here is what I have now:

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Basic HTML</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">

</script>

<style type="text/css">

</style>
<link rel="stylesheet" type="text/css" href="stats/picker.css" />
</head>
<body>
<?php
$vals = array(1, 2, 4, 3, 5, 6, 7, 13, 19, 22);

echo "<script type="text/javascript">
var vals = new Array(".join(',' , $vals).");

put_picker(vals);
</script>";
?>
</body>
</html>
[/CODE]


And my function is:

[CODE]
function put_picker(vals){

document.write('<div id="container" class="picker_container" style="width:239px">');
for (ev in vals){
document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div> ');
}
document.write('</div>');
}


[/CODE]


Am I missing something?
Copy linkTweet thisAlerts:
@FangOct 02.2008 — It would work in the way given. The array is global so you wouldn't need to pass it.
Copy linkTweet thisAlerts:
@ninedoorsauthorOct 02.2008 — Thanks Fang, huge help.


Nick
×

Success!

Help @ninedoors spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...