javascript - 使用 js 文件内的 phpfile 输出作为变量

标签 javascript php

最近我正在学习单页应用程序,但我遇到了一个问题,我正在开发的项目位于一个包含许多文件夹的文件夹内,php js 是主文件夹旁边的文件夹,每个文件夹都包含其类型的文件,问题是名为 getmax.php 的 php 文件之一给了我最大 id ,我想在名为 的 js 文件中使用此 max(id) module.js 为了给新模块提供下一个 id ,module.js 应该将此 id 提供给另一个名为 insert.php 的 php 文件,如果我手动设置 id,module.jsinsert.php 之间的连接工作正常。但我不知道如何让它使用 getmax.php 文件中的 max(id)

注意:我注意到最近我正在使用 MySQL,我应该使用 mysqli 我稍后会修复它。

getmax.php 是:

<?php
// alle relevanten Tabellen abfragen und als json zurückgeben.


$json["status"] = "running";
$details[] = "started get_tables ";
// Include confi.php
include_once('confi.php');
//var_dump($_POST);
$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, create_user 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["create_user"] = $max["create_user"];
            $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 .= ' GROUP BY create_user' ;
                $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 和 JS 分别在服务器和客户端上运行,因此您无法从其中之一调用另一个的方法/函数。 AJAX 的存在是为了在 JS 和服务器端代码之间传递值。

关于javascript - 使用 js 文件内的 phpfile 输出作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641663/

相关文章:

php - 使用mysql和php处理多个用户评论表单

javascript - 如何解决 Cypress 中的悬停问题?

javascript - 基于配置的Webpack多重 bundle

php - MySQL JOIN 按 ID 评级到按 ID 详细信息

php - 将数据从php中的CSV文件插入mysql

php - 检查产品的可用性

javascript - SweetAlert2 和 promise

javascript - 为什么这个 JavaScript RegExp 会产生双重答案?

javascript - 使用 jquery datepicker 格式化日期

javascript - 我应该如何使用 html、jQuery 构建一个控件数组