0

I know its REALLY simple, but I'm a newbie. I want to pass a variable from a PHP query via JS (for open-flash-chart) to the data file which is PHP.

This is my JS inside my PHP file. I'm pretty certian my issue is with the JS part since I've never used it before.

<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">

var player = "<? echo $player1 ?>";

swfobject.embedSWF(
"open-flash-chart.swf", "my_chart", "900", "350", "9.0.0", "expressInstall.swf", {"data-file":"data2.php"} );  

</script>

I am trying to pass the $player variable to the graph data file which is this ( data2.php)

<?php
include("include/session.php");

include 'open-flash-chart/php-ofc-library/open-flash-chart.php';

if  (isset($_GET['player'])) {
$player = $_GET['player']; }


$sql = "SELECT pos FROM nflscore where username = '$player'";
  • 2
    You could use [JSON][1] with the PHP function `json_encode` [1]: http://stackoverflow.com/questions/682260/returning-json-from-php-to-javascript – Art Planteur Nov 25 '12 at 00:24

3 Answers3

0
data2.php?variable="whatever variable"

on the php side do

$variable=$_GET["variable"];

now variable is the variable that you passed and you can do what you want with it

Dnaso
  • 1,335
  • 4
  • 22
  • 48
0

Assuming you want to use the var player change this in your swfobject.embedSWF call

{"data-file":"data2.php?player=" + player }
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
0

Well it pretty much depends on your needs!

Will javascript alter that variable? if not, the best way to transfer data between php pages are sessions! there are other options such as cookies, get vars, post vars etc. but users may change them putting your script in an uncomfortable position of dealing with wrong information if users do!

Using sessions your first page would simply look like this:

<?php session_start(); $_SESSION['player'] = $player; ?>
<!-- [...] -->
<script type="text/javascript">
    swfobject.embedSWF("open-flash-chart.swf", "my_chart", "900", "350", "9.0.0", "expressInstall.swf", {"data-file":"data.php"} );  
</script>
<!-- [...] -->

session_start has to be put at the very beginning of your php file, no headers should be sent before that function!

Well, your data file would then just become something like this:

<?php session_start();

/* ...your includes and the rest... */

$sql = "SELECT pos FROM nflscore where username = '{$_SESSION['player']}'";

Using cookies instead is quite the same, you just don't have to start the session at the beginning of your script (but if its a game you should rely on them already)! whats best though, you can access the cookie afterwards in your javascript as well!

so that's what your first page should look like :

<?php setcookie('player', $player) // somewhere in the script ?> 
<!-- [...] -->
<script type="text/javascript">
    swfobject.embedSWF("open-flash-chart.swf", "my_chart", "900", "350", "9.0.0", "expressInstall.swf", {"data-file":"data.php"} );  
</script>
<!-- [...] -->

an your data page also becomes :

$sql = "SELECT pos FROM nflscore where username = '{$_COOKIE['player']}'";

The third easy option is to just drop the information where it should be right from the beginning ;) so no sessions, no cookies but just plain get variables!

first file :

<!-- [...] -->
<script type="text/javascript">
    swfobject.embedSWF("open-flash-chart.swf", "my_chart", "900", "350", "9.0.0", "expressInstall.swf", 
    {"data-file":"data.php?player=<?php echo $player ?>"} );  
</script>
<!-- [...] -->

second file :

$sql = "SELECT pos FROM nflscore where username = '{$_GET['player']}'";

Things become different though if your javascript needs to change the variable's content

ivoputzer
  • 6,427
  • 1
  • 25
  • 43