I can't pass multiple variable from PHP (after fectch data from mysql) to js extenal file. So if I use echo $x_1
it can pass to #watts_value
in html file and display normally.
But I want to pass variable $x_1
to #watts_value
and $x_2
to kwh_value
. I already try to do that but it just pass all value to the #watts_value
.
I have no idea about how to pass variable from php to javascript. Please suggest me or tell me how to do this.
This is my html
<div id="watts_value"></div>
<div id="kwh_value"></div>
This is my main.js
$(document).ready(function (){
function read(){
$.post("get_db.php",
function(data, status){
if(status == 'success'){
$('#watts_value').html(data);
}
else{
$('#info').html("Error!");
}
});
};
read();
setInterval(read,1000);
});
This is my php file
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="rt_db";
$tbname="rt_data";
$conn=new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("Connection failed: ".$conn->connect_error);
}
$sql_1="SELECT * FROM $tbname WHERE SensorId ='1'";
$sql_2="SELECT * FROM $tbname WHERE SensorId ='2'";
$result_1=$conn->query($sql_1);
$result_2=$conn->query($sql_2);
if($result_1->num_rows>0){
while($row=$result_1->fetch_assoc()){
$x_1=$row['SensorData'];
echo $x_1;
}
}
if($result_2->num_rows>0){
while($row=$result_2->fetch_assoc()){
$x_2=$row['SensorData'];
echo $x_2;
}
}
else {
echo "fail";
}
$conn->close();
?>