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 7 of 7

Thread: Use of eval?

  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Use of eval?

    I don't have much ajax experience, maybe someone here can help.

    I'm working on a script that relies on data from a query to build a bar graph. Everything is working fine, but my kludgy solution makes me shudder a bit. I'm currently making an ajax call to a php script where I pass in the SQL. The PHP queries the database, does some processing with the returned data, and outputs a response that looks something like this...

    Code:
    //php
    the_output = "[3, 'a', '#000000'], [6, 'b' , '#111111'], [9, 'c' , '#222222']";

    When it returns to the js, I use eval to process this into an array that the bar graph plugin uses.
    Code:
    //js
    success: function(return_value){
    the_array = eval('new Array(' + return_value + ')');
    This works... but it's so ugly I have to believe there's a better way to pass the data between php and the js script (?)


    I do need the ajax functionality so I can dynamically load different data sets without reloading the page. Just thought I would mention that, I'm not just using ajax for the sake of using it.

    Thanks for any insights...

  • #2
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    I doubt that code works, PHP variables all begin with $.

    As for your query, yeah, just write the values in to the javascript

    PHP Code:
    the_array = [<?php echo $the_output?>];
    which when viewed in the will look like.

    PHP Code:
    the_array = [[3'a''#000000'], [6'b' '#111111'], [9'c' '#222222']]; 
    an array of arrays.

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Don't doubt it, it does indeed work. Chances are I probably just quickly typed that example in and left out a $... it's a crazy world we live in.

    Regarding your answer, I don't think you have grasped my question at all. I have no interest in changing the format of the return value. That's the format that my graph plugin expects, and it works correctly as shown in my post.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Why not have the PHP generate it as a JSON string instead and then use JSON.parse() to process it instead of eval().
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    mothra (02-11-2012)

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    felgall, that sounds like it's probably the right solution. I don't know anything about JSON however. I'm fairly inexperienced with AJAX in general. Would you mind giving me an example?

    Thank you.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome, thank you. I am off to absorb, will update with my findings ........


    To follow up, JSON simplified what I wanted to do, and it doesn't look and feel like a total kludge any more. For the sake of anyone else who might read this in the future, this is what I did...

    Code:
    // php
    while ($row = mysql_fetch_assoc($result)) {
    
    	$value = $row['val'];
    	$label = $row['label'];
    	$color = $row['color'];
    
    	$ret_ar[] = array($value, $label, $color);
    }
    
    echo json_encode($ret_ar);
    Code:
    // js
    success: function(return_value){
    dataArrayForGraph = JSON.parse(return_value);

    Works great, no more need to build ugly strings to later be used with the dreaded eval function. Thanks to felgall for turning me on to JSON!
    Last edited by mothra; 02-11-2012 at 12:49 AM.


  •  

    Posting Permissions

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