Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing array from php to javascript

    trying to pass several sql query array to further work in JS:

    PHP Code:
    <?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:

    Code:
    <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.

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by Cyberpops View Post
    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 :
    Code:
           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:
    Code:
    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
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The point is, that full JS looks like as such:

    Code:
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    from inputs give strings, use Number(a.value) instead of just a.value...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All works now, thanx


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •