2014-03-05 193 views
0

我有我的主要索引頁,它使用ajax請求來處理用戶登錄數據的外部php文件。在外部PHP文件我還包括其它外部PHP文件處理所有的功能,但外部登錄文件無法使用任何功能從另一個外部php腳本運行外部php腳本

這裏是從的index.php

$('#ind_login_submit').on('click', function (e) { 

    var vars = $("#ind_login_form").serialize(); // the script where you handle the form input. 
    //alert("gu"); 
    var hr = new XMLHttpRequest(); 
    hr.open("POST", "scripts/index/ind_login_submit.php", true); 
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    hr.onreadystatechange = function() { 
     if(hr.readyState == 4 && hr.status == 200) { 
      var data = JSON.parse(hr.responseText); 
      for(var obj in data){ 
       if(obj == "error"){ 
        alert(data[obj]); 

       }else if(obj == "success"){ 
        alert(data[obj]); 
        window.location.replace("http://localhost/site/dashboard.php"); 
       } 
      } 
      //alert(hr.responseText); 
      //location.load(); 
     } 
    }; 
    hr.send(vars); 
    //results.innerHTML = "requesting..."; 
    event.preventDefault(); 
}); 
Ajax調用

這裏是外部ind_login_submit.php

header("Content-Type: application/json"); 
session_start(); 
    include '../../connect.php'; 
    include '../functions.php'; 
    $secret_key = ''; 
globalSecret($secret_key); 
$error_array = array('error' => $secret_key); 
      $jsonData = json_encode($error_array); 
      echo $jsonData; 
      exit; 

if(isset($_POST['ind_login_remember'])){ 

    $ind_login_remember=1; 
}else{ 

    $ind_login_remember=0; 
} 



$ind_login_email = $_POST['ind_login_email']; 
$ind_login_password = $_POST['ind_login_password']; 

這裏是functions.php的

function globalSecret(){ 

$secret = "This is the secret"; 
$secret_key = sha1($secret); 
} 

當我運行代碼,我只是得到了一個空白警報顯示,它應該顯示$ SECRET_KEY變量

+1

跟蹤您的ne中的請求twork日誌。 PHP的迴應是什麼?暫時忘記警報 - 這是您的第一個調試端口。 – Utkanos

+1

你不用在函數中使用$ secret_key做任何事情,你是否忘記了'return'或者'echo' – 2014-03-05 22:37:29

+0

就像@Dagon建議你需要返回'$ secret_key'。該功能似乎是一個「空白」功能。 –

回答

0

我覺得行

globalSecret($secret_key); 

應該

$secret_key = globalSecret(); 

和功能globalSecret()應如下所示:

function globalSecret(){ 
    $secret = "This is the secret"; 
    $secret_key = sha1($secret); 
    return $secret_key; 
} 
+0

他需要從函數中返回一些東西。 – Barmar

+0

不會做任何事情,因爲'globalSecret'不會返回任何東西 – 2014-03-05 22:41:06

+0

好吧,現在完美的工作,我完全忘了返回它,只是認爲它會奇蹟般地發回變量 – Arken