SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ajax newbie question - changing ReadyStateby mouse click

    Hi, I've been reading on Ajax today for the first time and pretty much got the hang of it. The examples in the tutorials are all pretty much the same: onChange triggers a change in ReadyState that runs a php function. for example:

    http://www.tizag.com/ajaxTutorial/aj...ttprequest.php

    Im looking for a way to get a mouse click on a link to process the php script, but OnClick() does not affect ReadyState... how can i get a mouse click to change the ReadyState or run a phpscript?

    Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe i'll just explain what im trying to do:

    I have a 'delete' button with a JS pop up to confirm the delete. However, I also want to run a check on a MYSQL table so that it wont be possible to delete if that table is not empty. for that i need some kind of AJAX/JS function that will run once the "Delete" hyperlink is clicked.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    surely if you just have the javascript function in the onClick() it will work like other ones?

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry Munky i dont follow... The JS function called by OnClick needs to pull data from a mysql DB in order to decide what to do. I understand that AJAX is the way to do it, yet AJAX relies on ReadyState and Onclick wont change its value. If i cant change ReadyState i can't initiate a request.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, here is the code i have. each time the hyperlink is clicked the php script is executed and depending on the value of var it returns a result (and displays it). This part is working fine. Now all im trying to do is get the function to return a TRUE/FALSE value based on the value returned by ajaxRequest.responseText that will determine whether or not the hyperlink will actually work. cant seem to make it work though.

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    //Browser Support Code
    function ajaxFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
    
    		output = ajaxRequest.responseText;
    		}
    	}
    	ajaxRequest.open("GET", "phpscript.php?var=1", true);
    	ajaxRequest.send(null);
    
    return false;
    }
    
    //-->
    </script>
    
    <a href="aaaaaa" onClick="return ajaxFunction()">Test</a>

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, best thing to do, is in the :

    ajaxRequest.onreadystatechange = function(){

    }

    add:

    if ( ajaxRequest.responseText == "bad text"){
    document.getElementById("divname").innerHTML = "False";
    }else{
    document.getElementById("divname").innerHTML = "True";
    }

    Hope that helps


Bookmarks

Posting Permissions

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