...

View Full Version : AJAX calling seemingly random and nonexistent version of a PHP page.



The Noob Coder
08-07-2012, 11:18 PM
I am using ajax to call a php page, do a mysql query and return a simple echo. But the strangest thing seems to happen. The ajax seems to call a previous versions of my PHP (ie, an older version with different code and echo statements in them). This happens randomly and only with some inputs over others. Also, the version of the PHP changes too! Sometimes it'll call one that existed yesterday, one that existed 3 days ago or one that existed an hour ago. None of them exist anymore. They've been overwritten.

I am thinking there might be temporary files in the same directory as the most current PHP file that ajax is confusing with. Is there any way to know with Filezilla?

mlseim
08-07-2012, 11:37 PM
Can you show us some scripting?
No such thing as temporary files (that I know of).

The Noob Coder
08-08-2012, 01:12 AM
Here's the JS that makes up the AJAX. There's virtually nothing in the PHP page, except for a few echoes. What happens is that I'll change what is being echoed and when the AJAX returns the echo string from PHP it'll be a string that was only in a previous version of the file.



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type='text/javascript' >

var http;

function setXMLHttpRequest()
{
if(window.XMLHttpRequest)
http = new XMLHttpRequest();
else if(window.ActiveXObject)
http = new ActiveXObject("Microsoft.XMLHTTP");

url = "http://www.convolutedconstruct.com/Ajax/checkemail6.php?email=" + document.getElementById('email').value;
http.onreadystatechange = display;
http.open("GET", url, true);
http.send(null);

}

function display()
{
if (http.readyState == 4)
{
infostr = http.responseText;
alert(infostr);

if(infostr == "true")
{
jQuery(document).ready( function() {
jQuery("#emailForm").slideUp('slow');
jQuery("#Image1").fadeIn(3500);

});
}
else if(infostr == "invalid")
{
//Tell user they provided an invalid email address
jQuery(document).ready( function() {
jQuery("#invalidMessage").slideDown();

});
}
else
{
//Email was valid but not in the database.
jQuery(document).ready( function() {
jQuery("#emailNotRecorded").slideDown();

});
}
}
}
});

</script>


You'll notice that the URL being called is checkemail6.php. So far renaming the PHP file after an edit and then FTPing it was the only way to solve the problem (or at least circumvent it). It's as if there are multiple different copies of the original checkemail.php file in the directory and the JS is conflicted as to which one to choose. Does that make sense?

AndrewGSW
08-08-2012, 02:28 AM
Try


var encmail = encodeURIComponent(document.getElementById('email').value);

url = "http://www.convolutedconstruct.com/Ajax/checkemail6.php?email=" + encmail;

The Noob Coder
08-08-2012, 10:01 PM
Try


var encmail = encodeURIComponent(document.getElementById('email').value);

url = "http://www.convolutedconstruct.com/Ajax/checkemail6.php?email=" + encmail;

Before I put it into my code, could I trouble you to please explain what that first line does? Thank you. :D

AndrewGSW
08-08-2012, 10:05 PM
Before I put it into my code, could I trouble you to please explain what that first line does? Thank you. :D

It just creates a temporary variable to store the result after encoding the email text. Google encodeURIComponent.

The Noob Coder
08-08-2012, 10:32 PM
I added the encoded email, but it didn't work. It is calling and receiving data from an outdated version of the PHP file.

AndrewGSW
08-08-2012, 10:46 PM
Check the status as well:


alert(http.status);

The Noob Coder
08-08-2012, 10:47 PM
Where should this go?

Inigoesdr
08-09-2012, 04:03 AM
Since you are already including jQuery it's easier to do the AJAX with $.get() (http://api.jquery.com/jQuery.get/):


(function($) {
$(function() {

var url = "http://www.convolutedconstruct.com/Ajax/checkemail6.php";
var input = {email: $('#email').val()};
$.get(url, input, function(data){

infostr = $.trim(data);

if(infostr == "true")
{
$("#emailForm").slideUp('slow');
$("#Image1").fadeIn(3500);
}
else if(infostr == "invalid")
{
//Tell user they provided an invalid email address
$("#invalidMessage").slideDown();
}
else
{
//Email was valid but not in the database.
$("#emailNotRecorded").slideDown();
}
});

});
})(jQuery);
Not tested, but you should get the idea. Also note that AJAX requests can be cached just like regular pages, so if you notice an issue with items being cached you can append a timestamp or random number to prevent the request from being cached.

The Noob Coder
08-09-2012, 06:30 AM
Caching . . . is that what's going on? So if I put a timestamp on the JS, will that affect the PHP, too? Is there a way to just clear the cache on the PHP?

Inigoesdr
08-09-2012, 06:28 PM
Is there a way to just clear the cache on the PHP?

Not exactly, but you can prevent it from being cached in the first place by sending expired cache header() (http://php.net/header#example-4130)s.

The Noob Coder
08-09-2012, 11:03 PM
I added the header to disable the cached entries, but it still seems to call on previous versions of the file. I'll somethings and get back and let you know.

Fou-Lu
08-09-2012, 11:41 PM
Send a timestamp along with the the querystring request.
Caching can occur at many different steps, and may or may not be ignored by the device.

The Noob Coder
08-09-2012, 11:57 PM
Nevermind. It worked. I had another bug that was interfering with it. Thank you for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum