Hi all,
How do I pass a php variable e.g. $var into below javascript Javascript array?
Javascript Array:
var notes = new Array();
Thanks.
Hi all,
How do I pass a php variable e.g. $var into below javascript Javascript array?
Javascript Array:
var notes = new Array();
Thanks.
yeah, I believe it's something along those lines
<?php
$var = 'foo';
?>
var notes = new Array();
notes[0] = '<?php echo $var ?>';
Here is a tutorial for doing that.
This is my code...
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
/*print_r($javascript_array1);*/
}
<script language="Javascript">
// Define first array
var notes = new Array();
// Assign PHP value to Javascript array
var notes = '<?php print_r($javascript_array) ?>';
// Define second Array
var notes2 = new Array();
var notes2 = '<?php print_r($javascript_array1) ?>';
// Put all elements into a string
notes.join(",");
notes2.join(",");
document.write(notes[1]);
document.write(notes2[0]);
function change(id)
{
document.getElementById('inv_type_note').innerHTML = notes[id];
document.getElementById('inv_type_note2').innerHTML = notes2[id];
}
</script>
var notes and var notes2 display below in View Source...
var notes2 = 'Array
(
[0] => "2":"test"
[1] => "3":"testa"
)';
Any ideas? Thanks.
If you take a closer look at the link I gave, you'll see that you need to loop through the php array to fill the javascript array.
Here's my suggestion for doing it:
<?php
$id_array = array('id_zero', 'id_one', 'id_two');
$type_array = array('type_zero', 'type_one', 'type_two');
$type1_array = array('type1_zero', 'type1_one', 'type1_two');
for ($i = 0; $i < sizeof($id_array); $i++)
{
$php_array[$i] = $id_array[$i] . ':' . $type_array[$i];
$php_array1[$i] = $id_array[$i] . ':' . $type1_array[$i];
}
?>
<script language="Javascript">
var notes = new Array()
<?php echo php2js_array('notes', $php_array); ?>
var notes2 = new Array()
<?php echo php2js_array('notes2', $php_array1); ?>
document.write(notes[0]);
document.write(notes2[0]);
</script>
<?php
function php2js_array($js_arr_name, $php_arr)
{
$str = '';
foreach ($php_arr as $key => $val) {
if (is_int($key)) {
$str .= $js_arr_name . '[' . $key . ']';
} else {
$str .= $js_arr_name . '["' . $key . '"]';
}
if (is_int($val) || is_float($val)) {
$str .= '=' . $val . ';' . "\n";
} else {
$str .= '="' . $val . '";' . "\n";
}
}
return $str;
}
?>
Output
id_zero:type_zeroid_zero:type1_zero
Tried below but only echoes notes array in View Source...
//create string for javascript assoc array format. "key1:value1", "key2:value2", ........
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
}
?>
<script language="Javascript">
var notes = new Array();
var notes2 = new Array();
<?php
echo php2js_array('notes', $javascript_array);
echo php2js_array('notes2', $javascript1_array1);
?>
document.write(notes[0]);
document.write(notes2[0]);
function change(id)
{
document.getElementById('inv_type_note').innerHTML = notes[id];
document.getElementById('inv_type_note2').innerHTML = notes2[id];
}
</script>
<?php
function php2js_array($js_arr_name, $php_arr)
{
$str = '';
foreach ($php_arr as $key => $val) {
if (is_int($key)) {
$str .= $js_arr_name . '[' . $key . ']';
} else {
$str .= $js_arr_name . '["' . $key . '"]';
}
if (is_int($val) || is_float($val)) {
$str .= '=' . $val . ';' . "\n";
} else {
$str .= '="' . $val . '";' . "\n";
}
}
return $str;
}
?>
What I'm trying to achieve is this...
1/ Get data from Database
2/ Assign data to arrays
3/ When user makes a selection from select menu two ther textfields will be populated with the data from two relevant arrays.
Is there a simpler way of doing this?
Not like this
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
Like this:
$javascript_array[$i] = $id_array[$i] . ':' . $type_array[$i];
$javascript_array1[$i] = $id_array[$i] . ':' . $type1_array[$i];
I made that change for a reason. Why do you think you have to add the quotes to "$javascript_array" (which, btw, is really a PHP array)?
2/ Assign
data to [javascript] arrays[/QUOTE]This was your original problem, and that's the one I'm trying to help you solve.
Thanks. Nearly getting there...
In View Source...
notes[0]="2:testone";
notes[1]="3:testtwo";
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = $id_array[$i] .':'.$type_array[$i];
$javascript_array1[$i] = $id_array[$i] .':'.$type1_array[$i];
}
<script language="Javascript">
var notes = new Array();
var notes2 = new Array();
<?php
echo php2js_array('notes', $javascript_array);
echo php2js_array('notes2', $javascript_array1);
?>
document.write(notes[0]);
document.write(notes2[0]);
function change(id)
{
document.getElementById('inv_type_note').innerHTML = notes[id];
document.getElementById('inv_type_note2').innerHTML = notes2[id];
}
</script>
<?php
function php2js_array($js_arr_name, $php_arr)
{
$str = '';
foreach ($php_arr as $key => $val) {
if (is_int($key)) {
$str .= $js_arr_name . '[' . $key . ']';
} else {
$str .= $js_arr_name . '["' . $key . '"]';
}
if (is_int($val) || is_float($val)) {
$str .= '=' . $val . ';' . "\n";
} else {
$str .= '="' . $val . '";' . "\n";
}
}
return $str;
}
?>
Thanks again.