So I have this code :
function checkStatus()
{
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)
{
if (xmlhttp.responseText == "1") {
document.getElementById("maincontent").innerHTML = '<br/><br/><center>Please refresh the page to continue.</b></center>';
}
}
}
xmlhttp.open("GET","file.php?id=1",true);
xmlhttp.send();
}
and I'm wondering about the last 2 lines (xmlhttp.open and xmlhttp.send) , what is the function of these ? also when i go to file file.php?id=1 using the browser it only displays "0" whereas the the general function of the code is to redirect me to a website after i do a specific action and i believe the data is stored on file.php?id=1 but how can i see it from browser ?
Note: I'm not HTML/PHP programmer but i recognize the basics
The lines before xmlhttp.open() just create the XMLHttpRequest object that will handle the AJAX connection. Calling xmlhttp.open() is necessary to actually open the connection and xmlhttp.send() to send the request. Only after the request is sent, a response can be received and handled by the onreadystatechange handler.
This code looks rather obsolete, however. I would recommend not using XMLHttpRequest directly but rather use a library for it - see jQuery, for example.
Related
I find google bots cannot reach my web. In google webmaster tools, it said Truncated Response. I try to find reference in google about this but no other alternatives way to handle it. Then I try use ajax to load my part of web (content.php). I found this script in w3school:
<script>
$(document).ready(function(){
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","content.php",true);
xmlhttp.send();
});
</script>
content.php contains to display article from database.
I dont have some $ to buy dedicated server. please let me know how to minimize if you have other method.
Hey guys im trying to add to my site some ajax code. I know php but ajax is new for me so i used for an example on w3schools.com and changed some part of it. When i am using my script firebug gives me this error: TypeError: document.getElementById(...) is null. So what did i wrong i didn't know can you help me? This is my code:
function showOptionen(str)
{
if (str=="")
{
document.getElementById("Optionen").innerHTML="";
return;
}
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)
{
//Here in this line is the error:
document.getElementById("Optionen").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","bezeichnungs-optionen.php?q="+str,true);
xmlhttp.send();
}
I checked my bezeichnungs-optionen.php and its result is: <option value="DELL Notebook">DELL Notebook</option> so it is everything ok with it.
Many browsers will reject innerHTML that results in invalid markup.
Since you are returning an <option> tag make sure that your 'Optionen' is a <select> or <optgroup> tag.
You could try and call the function showOptionen(testStr) after the page has been loaded like this:
window.onload = function (){
showOptionen(testStr);
}
It seems like when the function is called the element does not exist, so it might not have been loaded yet.
You could always use the jQuery library, which simplifies many javascript operations, including ajax.
I have an embedded XMLHttpRequest problem;
The flow of data should go like this:
Interface.php uses modifyRecords.js (XMLHttpRequest) to call information from modifyRecords.php, which in turn uses showRecords.js (XMLHttpRequest) to call information from showRecords.php.
If I could somehow accomplish this, it would save a ton of code copying and/or rewriting.
When I backtrack to find where the errors are, there doesn't appear to be any problem showRecords.php and modifyRecords.php both load fine individually, just when AJAX calls another AJAX it totally breaks.
The nitty gritty is this:
My user interface is calling modifyRecords.js which is as follows;
function modifyRecords(cell,report,column,oldValue)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(cell).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","modifyRecords.php?report="+report+"&column="+column+"&oldValue="+oldValue,true);
xmlhttp.send();
}
modifyRecords.php is calling another AJAX function called showRecords.js;
function showRecords(str,column,nextDiv,oldValue)
{
if (str== null)
{
document.getElementById(nextDiv).innerHTML="----------------------------------------";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(nextDiv).innerHTML=xmlhttp.responseText;
document.getElementById(nextDiv).value = oldValue ;
}
}
xmlhttp.open("GET","showRecords.php?"+column+"="+str,true);
xmlhttp.send();
}
The problem is that each one of these works in isolation, however, when I use one AJAX function to call a page that is dependent on the second AJAX function, the records do not load.
I know this has something to do with synchronicity, because my only guess is that the referenced page has not finished loading when the referencing function is calling it.
I have tried setting the parameters to "false" for synchronous, instead of the usual asynchronous, however that breaks either function.
What would be the best solution?
I have considered combining all my AJAX functions, which would require a ton of rewrite to make them more generic, and functional for both environments.
Unless I'm much mistaken, you are trying to call your second AJAX script by setting the innerHTML of an element with the first?
Scripts aren't executed when added with innerHTML. Don't know why, it's been an annoyance to me, but you'd probably be better off separating HTML and JS, then putting the HTML in the innerHTML, and eval'ing the JS.
i want to pass js variable to php .my code is follows
function sub(uid){
window.location.href='<?php echo $urlp -> certificationceap(uid) ;?>';
here is some problem
You can't.
The browser makes a request
The webserver runs the PHP
The webserver delivers an HTTP resource to the browser
The browser parses the HTML and executes any JS in it
At this stage, it is too late to send data to the PHP program as it has finished executing.
You need to make a new HTTP request to get data back to it.
Probably something along the lines of:
function sub(uid){
location.href = 'redirect.php?uid=' + encodeURIComponent(uid);
}
check this thread How to pass JavaScript variables to PHP?
You can use ajax to achieve this..... advance apologies if it is not intended answer...
function send_var(js_var_1, js_var_2){
var parms = 'js_var_1='+js_var_1+'&js_var_2='+js_var_2;
if(js_var_1!='' && js_var_1!=''){
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){
//your result in xmlhttp.responseText;
}
}
xmlhttp.open("POST","<REMOTE PHP SCRIPT URL>",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(parms);
}
}
function download() {
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()
{ //alert(xmlhttp.readyState);
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert (xmlhttp.responseText);
}
}
xmlhttp.open("GET","import.php?file="+document.getElementById('uploaded_file').value,false);
xmlhttp.send();
// location.reload(true);
}
if I put alert and monitor xmlhttp.readyState then it shows me that its status does turn 4 and it does go in the if statement, if I don't monitor it with alert then it doesn't go in the if statement but I know import.php is working because I can see the changes in the database. I don't know whats going on...can anyone help.
Thanks
thats because the location.reload(true); is reloading the page, and not waiting for the xmlhttp call to complete
UPDATE:
try setting the 3rd parameter in the xmlhttp.open call to true
from the docs:
A Boolean indicator of whether the
call is asynchronous. The default is
True (the call returns immediately).
If set to True, attach an
onreadystatechange property callback
so that you can tell when the send
call has completed.
http://msdn.microsoft.com/en-us/library/ms757849%28v=vs.85%29.aspx