Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use a PHP variable inside of my JavaScript?
    primarykey
    data
    text
    <p>I think this is a case of me not knowing javascript, but I can't for the love of god get this to work</p> <p>For some reason, creating vars cancels out my java alert code. (maybe bc its wrong) And my java vars aren't being set correctly. </p> <p>I pointed out the problems in my comments</p> <p>In my SQL, I have Temperatures all with an associative value disk 'id'. So my data structure in this is:</p> <pre><code>$array[id]; $array[id]=array(); //For every new element //Using while ($row = mysql_fetch_assoc($result)) $array[id][]="temperature"; //second id $array[id2]; $array[id2]=array(); //For every new element $array[id2][]="temperature"; $array[id2][]="temperature2"; $array[id2][]="temperature3"; $array[id2][]="temperature4"; </code></pre> <p>MY ATTEMPT (WRONG CODE): //I simplified this code down. In my own version, the join works ONLY when I use an actual index "174" instead of a javascript variable that is 174. Couldnt get join to be alerted in this simplified version</p> <pre><code>&lt;?php $phparray=array(); $phparray["100"]="First Element"; $phparray["101"]="Second Element"; $phparray["102"]="Third Element"; $phparray["100"]=array(); $phparray["101"]=array(); $phparray["100"][]="First Element - Sub 2"; $phparray["100"][]="First Element - Sub 3"; $phparray["101"][]="Second Element - Sub 2"; echo $phparray["100"]; //Does not show 'First Element'. Shows Array echo $phparray["100"][0]; //Correctly shows sub element //var_dump($phparray); ?&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;Associative Array in PHP used in Java Test&lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var index=100; //var index2=&lt;?php echo $phparray[index]; ?&gt;; //Supposed to output 'First Element' var joined=[&lt;?php echo join($phparray[index], ', '); ?&gt;]; //Supposed to join elements of '100' alert("hello"); //This line does not work anymore after the var index2 made above &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container" style="height: 500px; min-width: 600px"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>EDIT: Here is the long full code of my php page:</p> <pre><code>&lt;?php include_once("../../config.php"); $conn=mysql_connect($dbhost,$dbuser,$dbpasswd) or die ('Error connecting to mysql'); mysql_select_db($dbname); ini_set('error_reporting', E_ALL); //ini_set('display_errors',1); ini_set('log_errors',1); $sql = "select disk_id from disk"; $result = mysql_query($sql); $ids = array(); $names=array(); $temperatures = array(); while ($row = mysql_fetch_assoc($result)) { $ids[] = $row['disk_id']; $temperatures[]=$row['disk_id']; //echo "&lt;br&gt;".$row['disk_id']; } // foreach ($ids as $value) { //echo "--START ".$value."--&lt;br&gt;"; $sql = "select * from disk_data WHERE disk_id=".$value; $result = mysql_query($sql); $dates=array(); $key = array_search($value, $temperatures); $temperatures[$value] = array(); //var_dump($temperatures); while ($row = mysql_fetch_assoc($result)) { $temps = $row['Temperature']; $temp = explode("||", $temps); $prehex=$temp[3]; $posthex=hexdec(substr($prehex,-2)); $predate=$row['create_date']; $postdate =strtotime($predate)*1000; $output="[".$postdate.", ".$posthex."]"; //$temperatures[$key][] = $output; $temperatures[$value][] = $output; $dates[]=$row['create_date']; //echo $row['create_date']." ".end($temperatures[$key])."&lt;br&gt;"; } } print_r(array_keys($array)); var_dump($temperatures); foreach ($ids as $value) { //echo $value; $key = array_search($value, $temperatures); //echo "Key: $key; Value: $temperatures[$value]&lt;br /&gt;\n"; $comma = join($temperatures[$value],", "); echo $comma; echo "\n"; } ?&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;Highstock Example&lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function() { var seriesOptions = [], yAxisOptions = [], seriesCounter = 0, //names=[&lt;?php echo join($ids, ', '); ?&gt;], names=["174"], //Above code works. BUT only using ID 174 to test values=[&lt;?php echo join($temperatures["174"], ', '); ?&gt;], //Supposed to be ALL data. But temp 174 colors = Highcharts.getOptions().colors; //alert(values); $.each(names, function(i, name2) { //alert(seriesOptions.length); alert(name2.toString()); //Works.... var values=[&lt;?php echo join($temperatures[name2], ', '); ?&gt;]; //Doesnt work //alert(values); console.log(values); //document.write(values); seriesOptions[i] = { name: name2, data:values }; // As we're loading the data asynchronously, we don't know what order it will arrive. So // we keep a counter and create the chart when all the data is loaded. seriesCounter++; if (seriesCounter == names.length) { createChart(); } }); // create the chart when all data is loaded function createChart() { chart = new Highcharts.StockChart({ chart: { renderTo: 'container' }, rangeSelector: { selected: 0 }, title: { text: 'Test Performance Data', style: { margin: '10px 100px 0 0' // center it } }, yAxis: { title: {text:'Temperature (°C)'}, labels: { formatter: function() { return this.value + ''; } }, plotLines: [{ value: 0, width: 2, color: 'silver' }] }, plotOptions: { line: { gapSize: 0 }, series: { //compare: 'percent' } }, tooltip: { pointFormat: '&lt;span style="color:{series.color}"&gt;{series.name}&lt;/span&gt;: &lt;b&gt;{point.y}&lt;/b&gt; ({point.change}%)&lt;br/&gt;', yDecimals: 2 }, series: seriesOptions }); } }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript" src="../js/highstock.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="../js/themes/gray.js"&gt;&lt;/script&gt; &lt;div id="container" style="height: 500px; min-width: 600px"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload