I am using AJAX to register users to a service. Following is the markup of submit button.
我正在使用AJAX将用户注册到服务。以下是提交按钮的标记。
<input type="button" id="register" name="register" class="btn btn-success" onclick="registration();" value="Register"/>
and Following is registration() function and other javascript-
以下是registration()函数和其他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");
}
}
}
Following is a snippet of my php file-
以下是我的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();
}
Now the problem is that even though my php script is able to write to Database it is not redirecting to otp.php i.e. header function is not working. Please help me out here on how can i redirect from my php script to otp.php
现在的问题是,即使我的php脚本能够写入数据库,它也不会重定向到otp.php,即标头功能不起作用。请帮我这里,我如何从我的PHP脚本重定向到otp.php
P.S. - The button is on a bootstrap Modal.
附: - 按钮位于bootstrap Modal上。
3 个解决方案
#1
0
Redirecting a AJAX request won't change the page that the request was sent from. Redirects happen before the page is ever sent to the client, but if the client is sending an AJAX request, obviously they've already been sent the page.
重定向AJAX请求不会更改发送请求的页面。重定向发生在页面发送到客户端之前,但如果客户端发送AJAX请求,显然他们已经发送了页面。
What you could do is return the redirect url back to the client as the AJAX response, then use Javascript to change the page using something like:
你可以做的是将重定向url作为AJAX响应返回给客户端,然后使用Javascript来改变页面,例如:
var ajaxResponse =...
...
location.href = ajaxResponse.responseText();
#2
0
Instead of using header()
server-side, use location.replace
redirect to otp.php
inside your ajax success callback
而不是使用header()服务器端,使用location.replace重定向到你的ajax成功回调中的otp.php
#3
0
As others said you are redirecting the request, not the current page. So on success of the php code, return something like this
正如其他人所说,您正在重定向请求,而不是当前页面。所以关于php代码的成功,返回这样的东西
echo json_encode( array( 'redirect' => 'http://your location' ));
then inside the ajax success grab that redirect
from data and simply do
然后在ajax成功抓取从数据重定向,简单地做
$.post('location', {requestdata}, function(data){
if( data.hasOwnProperty( 'redirect' ) ){
window.location.href = data.redirect;
}
});
This way you can tell the calling page where to send the client to.
这样您就可以告诉调用页面将客户端发送到的位置。
#1
0
Redirecting a AJAX request won't change the page that the request was sent from. Redirects happen before the page is ever sent to the client, but if the client is sending an AJAX request, obviously they've already been sent the page.
重定向AJAX请求不会更改发送请求的页面。重定向发生在页面发送到客户端之前,但如果客户端发送AJAX请求,显然他们已经发送了页面。
What you could do is return the redirect url back to the client as the AJAX response, then use Javascript to change the page using something like:
你可以做的是将重定向url作为AJAX响应返回给客户端,然后使用Javascript来改变页面,例如:
var ajaxResponse =...
...
location.href = ajaxResponse.responseText();
#2
0
Instead of using header()
server-side, use location.replace
redirect to otp.php
inside your ajax success callback
而不是使用header()服务器端,使用location.replace重定向到你的ajax成功回调中的otp.php
#3
0
As others said you are redirecting the request, not the current page. So on success of the php code, return something like this
正如其他人所说,您正在重定向请求,而不是当前页面。所以关于php代码的成功,返回这样的东西
echo json_encode( array( 'redirect' => 'http://your location' ));
then inside the ajax success grab that redirect
from data and simply do
然后在ajax成功抓取从数据重定向,简单地做
$.post('location', {requestdata}, function(data){
if( data.hasOwnProperty( 'redirect' ) ){
window.location.href = data.redirect;
}
});
This way you can tell the calling page where to send the client to.
这样您就可以告诉调用页面将客户端发送到的位置。