I have an AJAX code which takes input from the radio form.
When the server code satisfies a certain type of if() condition, it gets redirected to other page of my site for example, dashboard.php !
When it redirects, I still see the radio button form on the dashboard.php ! (which is not coded on that page...)
http://oi60.tinypic.com/30u7haq.jpg
For redirecting, I have used php function :
header('location:dashboard.php');
I need to somehow clear the response or disable the response when the code is redirected to the dashboard.php
AJAX Code:
function sendid(attack)
{
var att;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
document.getElementById("r1").checked = false
document.getElementById("r2").checked = false
}
}
xmlhttp.open("POST","battleuser2.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("att="+attack);
}
Hi I think you'll need to do your redirect after the ajax completes, not in the code that handles the ajax request...
try...
window.location.href = 'dashboard.php';
in your ajax success handler...
and remove the...
header('location:dashboard.php');
in your ajax handler
UPDATE:
Ajax handler (battleuser2.php)
if(true){
echo 'success';
}else{
echo 'fail';
}
exit;
Ajax complete:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if(xmlhttp.resonseText == 'success'){
window.location = 'dashboard.php?msg=You Won!';
}else{
window.location = 'dashboard.php?msg=You Lost!';
}
}
Dashboard.php..
if(isset($_GET['msg']))
echo $_GET['msg'];
Your ajax function is not loading a whole new page into the browser and thus the browser is not automatically processing the headers in the ajax response for a redirect. So, from your Ajax call, the server can't automatically cause the browser to go to a new page.
If you want that to happen, you will need to stop using an ajax call and just use a regular form post (where the server returns the response page which will be processed for a redirect).
Or, you can do the redirect manually in your own javascript that processes the result of the ajax call. Doing it yourself in your javascript code would look like this:
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
window.location = 'dashboard.php';
}
Related
So What I'm trying to do is, create an alert using java script. And if the person presses OK
it will send a variable to the handler and the handler will send an email. The following code is what I have, but I'm having trouble debugging it. As of right now I have this:
THE IF STATEMENT, CLICKING OKAY:
var pressed = confirm("Click okay to email users of video upload or Cancel to keep adding videos");
if (pressed == true)
{
var variable = 44;
emailParticipants(variable);
alert ("email sent");
}
FUNCTION THAT SENDS TO HANDLER:
function emailParticipants(sessionID)
{
var ID = sessionID;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://www.website.ca/portal/folder/HandleEmail.php?sessID="+ID,true);
xmlhttp.send();
}
HANDLER:
$SessID = $_GET['ID'];
<?php
$toEmail = 'someone#hotmail.com';
$toName = "TEST TEST";
<script>alert ("works");</script>
.
..
....
The rest of the handler functing just sends the email. I can't catch where the mistake is.
Well, you are invoking the handler via AJAX. The alert() method is inside the page the handler returns ... but you aren't doing anything with that page. You make a request and forget about the response. The browser doesn't execute the response from an AJAX call, so the code with the alert never executes.
By the way, you ** never ** should send e-mails (or do any other change in the server but returning information) with a GET request. GET requests can be cached, or even prefetched, causing emails to be sent when not requested, or not be sent when requested. I'd change the request type to POST.
If you want your alert, you need to change your client code and add this:
xmlhttp.open("GET","http://www.website.ca/portal/folder/HandleEmail.php?sessID="+ID,true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('works!');
} else {
alert('Failed! ' + xmlhttp.statusText);
}
}
}
xmlhttp.send();
I think you are a bit lost about AJAX. You should read more
Can javascript act like a web service and return a parameter value received in a query string to the client that posted the query? I am trying to return a query parameter in C# with no success. For example, if the query string is http://www.mypage/service?hubchallenge=1234 what javascript code would be used to return the value 1234 to the client without returning the web page itself?
You should have to you AJAX for it in your page. It cannot be done without passing a request from the client.
The below javascript code has to be placed in the page which send request.
function test()//the function can be called on events
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for other browsers
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET"," http://www.mypage/service?hubchallenge=1234",true);
xmlhttp.send();
}
In javascript, you can get the url into a string like this:
var urlString=document.URL;
then you can parse out parameters like
var qs=urlString.split("?")[1];
var qsArray=qs.split("&");
I am sending chat message, and receiving them in particular div.. All read and write with ajax. Everything works fine in Chrome, but in firefox, it does not shows...
Here is my code :-
var xmlhttp = false;
function read_message() {
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatBox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",'http://<?php echo $domain; ?>/m.php?id='+<?php echo $id; ?>,true);
xmlhttp.setRequestHeader("Content-type","text/html");
xmlhttp.send();
}
$(document).ready(function(){
setInterval("read_message()", 800);
});
$domain is define in Header page and it is Same domain, where file is running... i.e,
if my chat page is in localhost than $domain is also localhost..
I am aware of same origin policy of ajax, but is this problem due to using http://.
I cannot leave http:// part as i am using url-rewriting and there my url is something like http://localhost/chat/user/anonymous so, if i use only m.php?id=1 than it tries to fetch page from
http://localhost/chat/user/m.php which obviously doesnot exist... it exist in http://localhost/m.php
if above mentioned point is error, is there any way that we can solve it, or any other better help would be great.
Thanks
When you fire two requests, if the second request fires before the first request resolves, xmlhttp.responseText in your onreadystatechange listener would point to the responseText of the second (unresolved) request when the listener for the first request fires. This is because you only use a single global xmlhttp variable that is shared for all requests.
If you define var xmlhttp inside your read_message, instead of outside of it, then each new function call will have its own private xmlhttp variable:
function read_message() {
var xmlhttp;
//...
}
I'm using zend 1.11 framework with doctrine . I'm sending below ajax request to populate dropdown list & etc... But once session expired i'm redirecting to login page in every actions of controllers. So i'm getting whole html page as response if session got expired. How to do that in javascript (how can i find out whole html response or not) . I'm using Zend_Auth for session management.
function loadzone(value)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("district_span").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/main/zonechange?zc="+value,true);
xmlhttp.send();
}
Well to my understanding:
A user is logged in and opens some form. You do AJAX-Checkups to see if user is still logged in. If user was idle for too long the session will expire.
In that case you want to redirect the user?
If is is like i think you can just give the user a bad callback. This can be done like this:
$this->_response->setHttpResponseCode(401); //401 = unauthorized
If you do so, you can just extend your JS Functionality to catch that code
if (xmlhttp.readyState==4 && xmlhttp.status==401) {
//do the redirect
}
Is this what you're asking for or did i misinterpret your question? :)
Additional Information regarding response codes can be found here: http://de.wikipedia.org/wiki/HTTP-Statuscode
I'm trying to load specific content from a XML to a HTML div. I'm using a function with parameters to do this.
This my call to function:
loadDoc("news.xml","destak-article","article");
this should send a request for the xml file, get the content of «article» tag and put it on the «destak-article» div.
Here's my function body:
function loadDoc(url,id,tagname){
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.getElementById(id).innerHTML = xmlDoc.getElementsByTagName(tagname)[0].childNodes[0].nodeValue;
}
But this doesn't seem to work.
On Chrome js console I get this error: Cannot call method 'getElementsByTagName' of null
On Firebug I get: xmlDoc.getElementsByTagName(tagname)[0] is undefined
Any help is much appreciated.
Did you verify the server response? Use some error checking in your code. For example:
if (xmlhttp.status == 200) {
document.getElementById(id).innerHTML = xmlDoc.getElementsByTagName(tagname)[0].childNodes[0].nodeValue;
}
else {
alert('error');
}
You need to register a handler function that will be called once the request is complete. You can see an example of how to do that here.
What's happening in your case is that you're trying to get the xmlDoc immediately after sending the request and the server hasn't had time to process the request and respond yet.