Richard Atterfalk
Richard Atterfalk

Reputation: 472

Fetching file with AJAX, can't read PHP

I have checked the suggestions that came up before posting, hope I didn't miss anything now.

I have a piece of code that I use to get txt-files for my website but now I need to redo the code so it gets both txt and php-files, but it just won't read the php-script. I'm a bit afraid to mess up the code at this moment so I'm walking on the safe side of the road and ask here if anyone knows a good add to the code. It's quite embarrasing that I still have codes for IE 5&6 in it, so if you wish to remove that at the same time, go ahead. I won't hate you for it, I promise.

UPDATE:
I have four files:
html - Calling the .js-file with the ajax-script.
js - With all my javascript(and simular)-codes.
php - That contains... Well, you get the point.

I have to call the php-code somehow, like I call my txt-files, but of course so the php works as it should. I am very new to AJAX, so I don't dare to mess around with this code at the moment, but I figured that I might be able to add some kind of if-statement that calles the php-file as it is intended to be.
But I have no clue what that code might be and where to put it for things to work accordingly. Any help would be appritiated and credited in the code, of course.

Heres the AJAX-code that is contained within the .js-file:

/*Load the link.*/
function loadXMLDoc(url)
{
    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("leftCol").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}
/*Highly unnecessary, but I wanted to see if it worked and it looks better on the .html-page.*/
function get_link(url)
{
  loadXMLDoc(url);
}

Upvotes: 0

Views: 594

Answers (2)

Dutchie432
Dutchie432

Reputation: 29160

As the above commenter said, it is best to use a 3rd party tool for such things - if for no other reason than to greatly increase cross-browser compatibility.

if you were to use jQuery, the code would be reduced to.

function get_link(url)
{
    $.ajax({url: url, success: success:function(result){
            //the code / resulting string will be in the result variable.
    }});
}

jQuery CDN Hosted: http://code.jquery.com/jquery-1.5.min.js

Let me ask this... if you change your code to

function get_link(url)
{
    window.location=url;
}

Does your web browser successfully navigate to the page you are trying to retrieve via AJAX? If not, there is likely a problem with your PHP syntax.

Upvotes: 3

HoLyVieR
HoLyVieR

Reputation: 11134

it just won't read the php-script

It's a rather vague statement, but here are few pointers that could be the solution :

PHP file are interpreted on the server so when you do an Ajax call to that page what you receive on the client side is the result of that php script, not his content.

You are assigning the result of the query directly in the HTML, if the result contains data that does not render anything, you won't see anything. For example the content <script>Text here bla bla bla</script> will just show nothing.

If you want to make sure you get some data back from a file, you can just alert the content when you receive it.

Make sure your path to your PHP page is correct. To detect if the file is not giving a 404 error code or any other error code, you can use this :

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            document.getElementById("leftCol").innerHTML = xmlhttp.responseText;
        } else {
            alert("Error " + xmlhttp.status);
        }
    }
}

Upvotes: 1

Related Questions