2 Replies - 1184 Views - Last Post: 12 January 2010 - 10:34 AM

#1 the_metro_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-December 09

calling a php function to access a database from javascript

Posted 12 January 2010 - 09:29 AM

i am mixing javascript and php to graph sales dynamically from a mysql database - unfortunately i can only plot 1st sales value - i basically need javascript to call a php function that queries the database and returns the next sales value - here is a snippet from the php file

$x=0;
echo "
function loop() {
	ctx.clearRect(0, 0, canvas.width, canvas.height);";

$qt=mysql_query("select sales from gd_graph");
$y = mysql_result($qt, $x); 
$x+=1;

echo "
		yy_prev=yy;
		yy=" . $y .";\n";

echo "
		for(var i = 0; i < NUM_PARTICLES; i++) {
		particles[i].update();

		ctx.moveTo(particles[i].x, yy);
		ctx.lineTo(particles[i].x - particles[i].xvel, yy_prev);
		ctx.stroke();
		ctx.closePath();
	}
	setTimeout(loop, 1000);
}



any ideas much appreciated

cheers

Abbey

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: calling a php function to access a database from javascript

#2 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: calling a php function to access a database from javascript

Posted 12 January 2010 - 10:21 AM

You should read about Ajax.
Was This Post Helpful? 0
  • +
  • -

#3 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: calling a php function to access a database from javascript

Posted 12 January 2010 - 10:34 AM

Javascript and PHP can't communicate directly. It's not like you can call a specific function from a PHP file from JS. But there's a way you can communicate. JS can load a php file using AJAX. And the php page can be dynamic with GET or even POST variables. So the php file should compute the output and then output data for the Javascript. The data outputted should be in XML.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1