Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question AJAX calling seemingly random and nonexistent version of a PHP page.

    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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,528
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Can you show us some scripting?
    No such thing as temporary files (that I know of).

  • #3
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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?

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Try

    Code:
    var encmail = encodeURIComponent(document.getElementById('email').value);
    
    url = "http://www.convolutedconstruct.com/Ajax/checkemail6.php?email=" + encmail;
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Try

    Code:
    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.

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by The Noob Coder View Post
    Before I put it into my code, could I trouble you to please explain what that first line does? Thank you.
    It just creates a temporary variable to store the result after encoding the email text. Google encodeURIComponent.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I added the encoded email, but it didn't work. It is calling and receiving data from an outdated version of the PHP file.

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Check the status as well:

    Code:
    alert(http.status);
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Where should this go?

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Since you are already including jQuery it's easier to do the AJAX with $.get():
    Code:
    (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.

  • #11
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #12
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by The Noob Coder View Post
    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()s.

  • #13
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #15
    New Coder The Noob Coder's Avatar
    Join Date
    Jul 2012
    Location
    The City that Never Sleeps
    Posts
    99
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind. It worked. I had another bug that was interfering with it. Thank you for your help.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •