我正在学习单页应用程序,我有以下php代码,该代码根据数据库中的create_user给我max(id)。我有一个单独的js文件,我想使用此js文件中的id。(。php文件存在于php文件夹中,.js文件存在于js文件夹中,而htlm位于视图中),请问我的问题很清楚如何使用值或php文件中的变量int spearate js文件不在html文件中,mysqli问题我稍后将学习,在此先感谢您的帮助,我该怎么做?

<?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);
?>

最佳答案

如果我正确理解了您的问题,则只希望将php json编码的结果作为javascript变量即可。如果是这样,只需将php代码注入您的javascript。

 <script type="text/javascript">
    var jsonArray = <?php echo json_encode($json) ?>;
 </script>

关于javascript - 在JavaScript中使用php的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34420959/

10-11 12:33
查看更多