Reputation: 97
I am learning single page application, I have the following php code which gives me the max(id) according to create_user
from the database. and i have a separate js file i want to use the id in this js file.(.php file exist in php folder , .js file in js folder and htlm in views), please do my question is clear how to use a value or variable from php file int spearate js file not in html file , mysqli issue i will learn later, thanks in advance for your help how do I do this ?
<?php
$json["status"] = "running";
$details[] = "started get_tables ";
include_once('confi.php');
$request_body = file_get_contents('php://input');
// first store the given set of data to keep it for future analysis
$statement = "INSERT INTO tbl_archive (content) VALUES ('$request_body' );";
mysql_query($statement);
$input = json_decode($request_body, true);
// now check if valid user
$user = $input["user"];
$username = $user["username"];
$password = $user["password"];
if($password and $username){
$mySQLstring = "SELECT username, password, id FROM tbl_user where username = '$username' ;";
$json["statement"][] = $mySQLstring;
$qur = mysql_query($mySQLstring);
//var_dump ( $qur );
if ($qur){
$max = mysql_fetch_assoc($qur);
}
if ($max){
$json["max"] = $max;
if ($max["password"] == $password){
$json["username"] = $username;
$json["id"] = $max["id"];
$json["status"] = "ok";
$tables = array("class", "class_user", "module", "module_class", "module_user", "rating", "student", "student_class");
//$tables = array("class");
foreach($tables as $table){
if ( $table == 'module' ){
$statement ='SELECT create_user, MAX(id) FROM tbl_'.$table;
$statement .= ' WHERE create_user = 19 ' ;
$qur = mysql_query($statement);
if ($qur){
while($r = mysql_fetch_array($qur, MYSQL_ASSOC)){
//var_dump($r);
//echo (json_encode($r));
$result[$table][] = $r;
}
}
}
}
$json = array("status" => "ok", "data" => $result);
}
}
}
@mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>
Upvotes: 0
Views: 70
Reputation: 2210
There are two ways you can do this. If the JavaScript is running on that page, you can just echo out the PHP variable into a JavaScript variable.
<script type="text/javascript">
var phpVar = <?php echo json_encode($json) ?>;
console.log(phpVar); // Your JSON as a JavaScript object
</script>
Or from within your PHP:
<?php
echo '<script type="text/javascript">var id='.json_encode($json).'</script>";
?>
If it's a different file, you can load the JSON through an AJAX call:
var xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var phpVar = JSON.parse(xmlhttp.responseText);
console.log(phpVar); // Your JSON as a JavascriptObject
}
}
xmlhttp.open("GET", "phpfile.php", true);
xmlhttp.send()
More on AJAX: http://www.w3schools.com/ajax/
Upvotes: 1
Reputation: 56
If I'm understanding your question correctly, you just want your php json encoded result as a javascript variable. If so, it's simply a matter of injecting php code into your javascript.
<script type="text/javascript">
var jsonArray = <?php echo json_encode($json) ?>;
</script>
Upvotes: 1