Embedded XMLHttpRequest (AJAX calling a page which also uses AJAX) - php

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.

Related

XMLHTTP Connection

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.

error in ajax/php

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.

Implementing twitter like user flow interface?

I want to implement twitter like user flow interface, i.e. when user goes to
http://twitter.com/#!/abc the page is loaded.
I have tried to implement this at:
http://forum.abhibhatia.co.cc/ajax/
What I try to do is that as soon as page loads, the url is splitted by "#!/" and secon part is loaded so that http://forum.abhibhatia.co.cc/ajax/posts.php can be accessed at http://forum.abhibhatia.co.cc/ajax/#!/posts.php . The problem I am facing is that the page does not change after I redirect user using window.location
it does not work. I have not tried any other way to redirect users yet.
here is the javascript function used:
function geturl(){
var url=window.location.toString();
var a=url.split("#!/");
if(a.length==1)
a[1]='data.php';
return a[1];
}
function fetchPage(){
var url=geturl();
var xmlhttp;
if (url.length==0&&m==1)
{ url='data.php';
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==1) document.getElementById("data").innerHTML="Loading...";
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var file=xmlhttp.responseText;//alert(file);
var contents=file.split("<head>");//alert(contents[0]);
var useable=contents[1].split("</head>");
var head=useable[useable.length-2];//alert(head);
var body=useable[useable.length-1].split("</body>");
document.getElementById("data").innerHTML=body[0];
document.head.innerHTML=document.head.innerHTML+head;
//document.getElementById("data").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
fetchPage();
look closer at the window.location object you can use window.location.hash but as mentioned before hashbangs are a bit old these days I'd be much more inclined to use the html5 state management with standard hashes # as a fallback.
some further reading:
https://developer.mozilla.org/en/DOM/window.location
http://www.adequatelygood.com/2010/7/Saner-HTML5-History-Management
If you really want to make a twitter style interface you should look to put the whole app on a framework which does all of this for you like backbone.js which will mean that you just need to use a REST/CRUD style data source and then will implement all of the state managemet and hash fallbacks for you.

How come only one of these functions will work at a single time?

Even if I remove the if statements, only one of these following will work at one time. To get the former to work, I have to comment out the latter.
<?
if(isset($_POST['region'])){
echo "<script> showRecords('".$_POST['region']."','region','country') </script>";}
if(isset($_POST['country'])){
echo "<script> showRecords('".$_POST['country']."','country','provice') </script>";}
?>
The script is referring to this:
function showRecords(str,column,nextDiv)
{
if (str=="")
{
document.getElementById(nextDiv).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)
{
document.getElementById(nextDiv).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get"+column+".php?"+column+"="+str,true);
xmlhttp.send();
}
The script leads to a very simple set of pages where that list some values based on some $_GET information.
I just cannot understand why it is only allowing me to do one of these scripts at a time. I even tried cloning the function to showRecords2, and it will still only do showRecords or showRecords2.
Replace xmlhttp=new XMLHttpRequest() with var xmlhttp=new XMLHttpRequest(). Notice the var keyword added. What happened is xmlhttp is becoming a global scope variable and it gets overwritten with new values/argument/parameters everytime you make a request e.g. calling showRecords twice while the first one is still doing stuff the second call overwrites it.
Remember to make all your variables in the function level to avoid overwrites unless they are actually gonna be used in the global scope. It's time consuming to debug this kind of issues especially when you don't know where to find stuff. Hope that helps!

javascript variable to php

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);
}
}

Categories