2016-05-31 139 views
0

我使用AJAX將用戶註冊到服務。 以下是提交按鈕的標記。爲什麼php的header()不工作?

<input type="button" id="register" name="register" class="btn btn-success" onclick="registration();" value="Register"/> 

和以下是註冊()函數和其他Javascript成爲

function registration() 
    { 
     var name = document.getElementById("regname").value;     
     var email = document.getElementById("regemail").value;    
     var pass = document.getElementById("regpassword").value;    
     var pass2 = document.getElementById("regreenterpassword").value;  
     var tagline = document.getElementById("tagline").value; 
     registerOnDB(); 
    }; 
    var xmlHttpReg = createXmlHttpRequestObject();    //Calling the function to vaidate input credentials 
    function createXmlHttpRequestObject()       
    { 
     var xmlHttpReg; 

     if(window.ActiveXObject)         //If user is using internet Explorer 
     { 
      try 
      { 
       xmlHttpReg = new ActiveXObject("Microsoft.xmlHttp"); 
      } 
      catch(e) 
      { 
       xmlHttpReg=false; 
      } 
     } 
     else              //If user is NOT using internet Explorer but any other browser 
     { 
      try 
      { 
       xmlHttpReg = new XMLHttpRequest(); 
      } 
      catch(e) 
      { 
       xmlHttpReg=false; 
      } 
     } 

     if(!xmlHttpReg)           //If Object can not be initialized. 
      { 
       alert("Can not create object"); 
      } 
     else 
     { 
      return xmlHttpReg; 
     } 
    } 
     function registerOnDB() 
     { 
      if(xmlHttpReg.readyState==0 || xmlHttpReg.readyState==4)            //If object state is either 0 OR 4 i.e. object not engaged otherwise. 
      { 
       var name = encodeURIComponent(document.getElementById("regname").value); 
       var email = encodeURIComponent(document.getElementById("regemail").value); 
       var pass = encodeURIComponent(document.getElementById("regpassword").value); 
       var tagline = encodeURIComponent(document.getElementById("tagline").value); 
       var parameters="name="+name+"&email="+email+"&pass="+pass+"&tagline="+tagline+"&loggedin=true"; 
       var url = "http://localhost/dashboard/x/php/register.php?"+parameters;       //Sending Data to php script for validation 
       xmlHttpReg.open("GET",url,true);                 //Preparing to send request 
       xmlHttpReg.onreadystatechange = handleServerResponseReg;           //Handling response that will come from php script 
       xmlHttpReg.send();                    //sending values to php script 

      } 
      else 
      { 
       setTimeout('registerOnDB()', 1000);                //If reasyState is NOT 0 or 4 then repeat then wait and check again after 1 second. 
      } 
     } 


    function handleServerResponseReg() 
    { 
     if(xmlHttpReg.readyState==4||xmlHttpReg.readyState==0)            //If object state is either 0 OR 4 i.e. object not engaged otherwise. 
     { 
      if(xmlHttpReg.status==200)                  //status 200 means everything went OK 
      { 
       document.getElementById("errorpromptregister").innerHTML = xmlHttpReg.responseText;   //Putting out message received from XML on the screen at a div whose ID is errorprompt. 
      } 
      else 
      { 
       alert("xmlHttp.status!=200"); 
      } 
     } 
    } 

以下是我的PHP的片段文件 -

if(mysql_query("INSERT INTO user_tbl(name, tagline, email, password, salt, otp) VALUES ('$name','$tagline','$email','$password','$salt', '$otp')")) 
     { 
      session_start(); 
      $_SESSION['email']=$email; 
      header("Location : otp.php"); 
      exit(); 
     } 

現在的問題是,即使我的PHP腳本能夠寫入數據庫,它不會重定向到otp.php 即頭功能不起作用。 請幫我在這裏,我怎麼可以從我的PHP腳本重定向到otp.php

附: - 該按鈕位於引導模式上。

+1

你正在做一個ajax調用。這意味着只有ajax調用本身,而不是調用來自的頁面,將被重定向。把某人送到商店去挑選某些東西,然後你去商店拿起同樣的東西,這是區別的。如果商店外出,並告訴你去其他地方,你和「某人」將被重定向,但你不會知道這個人被重新定向,直到他們回到家並告訴你。 –

+0

當您提出有關錯誤的問題時,**總是**發佈錯誤日誌。要啓用錯誤報告給你的php代碼,追加'error_reporting(E_ALL); ini_set('display_errors','1');'在腳本的頂部,它返回什麼? –

回答

0

重定向AJAX請求將不會改變該請求發自頁。重定向發生之前的頁面曾經被髮送到客戶端,但如果客戶端發送一個AJAX請求,顯然他們已經被髮送的頁面。

你可以做的是返回重定向URL返回給客戶端的AJAX響應,然後使用JavaScript喜歡的東西來改變頁面:

var ajaxResponse =... 

... 

location.href = ajaxResponse.responseText(); 
0

而不是使用header()服務器端,使用location.replace重定向到otp.php你的Ajax成功回調

0

內側,有人說要重定向請求,而不是當前頁面。因此,在PHP代碼的成功,返回這樣的事情

echo json_encode(array('redirect' => 'http://your location')); 

那麼阿賈克斯成功搶到就是redirect從數據裏面,只是做

$.post('location', {requestdata}, function(data){ 
    if(data.hasOwnProperty('redirect')){ 
     window.location.href = data.redirect; 
    } 
}); 

這樣你就可以告訴調用頁面中發客戶端。