Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-10-2012, 12:25 AM   PM User | #1
mothra
Regular Coder

 
Join Date: Jul 2003
Posts: 262
Thanks: 1
Thanked 0 Times in 0 Posts
mothra is an unknown quantity at this point
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...
mothra is offline   Reply With Quote
Old 02-10-2012, 12:41 AM   PM User | #2
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
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.
Cremator is offline   Reply With Quote
Old 02-10-2012, 12:56 AM   PM User | #3
mothra
Regular Coder

 
Join Date: Jul 2003
Posts: 262
Thanks: 1
Thanked 0 Times in 0 Posts
mothra is an unknown quantity at this point
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.
mothra is offline   Reply With Quote
Old 02-10-2012, 01:01 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
mothra (02-11-2012)
Old 02-10-2012, 01:12 AM   PM User | #5
mothra
Regular Coder

 
Join Date: Jul 2003
Posts: 262
Thanks: 1
Thanked 0 Times in 0 Posts
mothra is an unknown quantity at this point
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.
mothra is offline   Reply With Quote
Old 02-10-2012, 01:20 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
ISee http://javascriptexample.net/ajax04.php
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-10-2012, 01:22 AM   PM User | #7
mothra
Regular Coder

 
Join Date: Jul 2003
Posts: 262
Thanks: 1
Thanked 0 Times in 0 Posts
mothra is an unknown quantity at this point
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..
mothra is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 PM.


Advertisement
Log in to turn off these ads.