0

This is a part of the code in a 'process.php' file:

echo "<script type='text/javascript'> percYes = ".$yes."</script>"
echo "<script type='text/javascript'> percNo = ".$no."</script>"

This 'process.php' file runs in the background (using jQuery/ajax) when the user clicks a butto. The 'echoed' html above replaces the contents of a div. So essentially what I'm trying to do is update some Javascript variables using a background php call, the above solution does not seem to work though, i.e. the script is not being ran once it is placed in the div.

A bonus problem involves using these updated Javascript variables to update a graph. I have a workng javascript graphing function, but the problem is getting the new graph to replace the old one (or just update it, if that's possible).

Thanks.

3
  • what function are you using to make the ajax call. To parse the data as script , I guess $.load needs to be used and not $.ajax. Commented Jan 17, 2012 at 11:31
  • I really don't know too much about this stuff, but I'm using xmlhttp.open() and xmlhttp.send(). I'll have a look into $.load. EDIT: Wow, $.load looks perfect. Commented Jan 17, 2012 at 11:34
  • There is also jQuery's getScript to just load a script file. Commented Jan 17, 2012 at 11:44

1 Answer 1

1

If all you are trying to do is update some JS vars, you may be better off having your process.php file returning a json string:

$array = ('percYes' => $yes, 'percNo' => $no);
echo json_encode($array);

This will give you a json string that can be evaluated and used in a callback for your JS ajax call.

So if you have a var 'percYes' and 'percNo' in an accessible scope, your callback could look something like this:

function(jsonstr) {
  obj = eval(jsonstr);
  percYes = obj.percYes;
  percNo  = obj.percNo;
}

I hope this helps.

1
  • This sounds great! So when I want to update the js variables I call function(jsonstr)? Commented Jan 17, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.