2016-01-06 85 views
0

最近我正在學習單頁面應用程序,但我遇到了一個問題,我正在處理的項目是在一個包含多個文件夾的文件夾內,php js文件夾在主文件夾,並且每個文件都包含其類型的文件,問題在於其中一個名爲getmax.php的php文件給了我最大的id,我想在一個名爲module.js的js文件中使用這個max(id),以便爲新模塊指定下一個id,即module.js應該給這個id到另一個名爲insert.php的php文件,如果我手動設置ID,module.jsinsert.php之間的連接可以正常工作。但我無法弄清楚如何使用getmax.php文件中的max(id)使用phpfile輸出內部js文件文件作爲變量

注意:我最近注意到我在使用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); 
?> 
+2

要從php到javascript的價值,你可以使用AJAX。 –

回答

1

PHP和JS在服務器和客戶端上運行分別,因此你不能從另一個調用一個方法/函數。 AJAX存在於JS和服務器端代碼之間傳遞值。

+0

你知道任何網站或可能在youtube上簡單明瞭地教阿賈克斯嗎? –