...

View Full Version : passing array from php to javascript



Cyberpops
10-07-2012, 05:04 PM
trying to pass several sql query array to further work in JS:


<?php
$x = mysql_query("SELECT p_max FROM dsd_price");
$y = mysql_query("SELECT p_min FROM dsd_price");
$z = mysql_query("SELECT p_price FROM dsd_price");
while ($x_array = mysql_fetch_array($x)){$max[] = $x_array['p_max']; }
while ($y_array = mysql_fetch_array($y)){$min[] = $y_array['p_min']; }
while ($z_array = mysql_fetch_array($z)){$price[] = $z_array['p_price'];}
?>

JS:


<script type="text/javascript">
function price() {
var max = <?php echo json_encode($max); ?>;
var min = <?php echo json_encode($min);?>;
var price = <?php echo json_encode($price);?>;
document.write(price + " " + min +" "+ max );
}
</script>

Result:


[object Object],[object Object],[object Object],11,15,17 [object Object],[object Object],[object Object],1,6,11 [object Object],[object Object],[object Object],1,10,15

So the problem, as u can see is that it adds those [object, Object] values to my array.

rnd me
10-07-2012, 07:14 PM
trying to pass several sql query array to further work in JS:
[PHP]
So the problem, as u can see is that it adds those [object, Object] values to my array.

you are adding those in in php.
i don't think you need the 3 while statements.
you should be able to do something like :

echo json_encode(mysqli_fetch_all(MYSQLI_NUM));


and just dump the whole result set at once. if you want the columns renamed, do it in sql using "dbName AS myName" in the SELECT.


without going into the reasons why document.write() is bad, you can view your data by using JSON.stringify() instead of coercing the objects to a string.

this is the same as php's differences between echoing a var_dump($x) -vs- just echo $x.

try something like this to get a peek:

myData= [price , min , max ]

document.write(
'<pre>' +
JSON.stringiyfy(myData, null, "\t" )
)



or use firebug to inspect the variables in real-time without "printing" the data by replacing document.write() with console.log() or console.dir().

there are also several template libs out there than make it trivial to turn json into html using nice clean syntax. those can make debugging easier by offering advices in their error-handling routines.


cheers

Cyberpops
10-07-2012, 07:49 PM
The point is, that full JS looks like as such:


function price() {
var frm=document.dsd_form;
var a=frm.conPrice;
var b=frm.OverP;

var max = <?php echo json_encode($max); ?>;
var min = <?php echo json_encode($min);?>;
var price = <?php echo json_encode($price);?>;

for (counter=(min.length / 2); counter<(min.length + 1); counter++){

if(a.value >= min[counter]){
if(a.value <= max[counter]){
b.value=min[counter]+ " " + max[counter] + " " + price[counter];
}
else{b.value = 0;}
}
}
}

so, writing a number into conPrice input field, I want OverP field to change its value to a price following a logical statement.

rnd me
10-08-2012, 01:52 PM
from inputs give strings, use Number(a.value) instead of just a.value...

Cyberpops
10-08-2012, 10:39 PM
All works now, thanx :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum