Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 33

Thread: Calling Functions from Functions.

  1. #11
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Well the:

    Code:
    var req = makeRequest();
    Places one new request object in the global scope, available to all functions. Each one of your:

    Code:
    checkWhoIsWriting();
    getMessages();
    checkRooms();
    getRoomName();
    functions took that one request object and overwrote it's properties to be the one's it required.

    If you were to (and similar in the other functions):

    Code:
    function whoiswriting(){
    //document.getElementById("state").innerHTML = document.chatform.guestname.value + " is Writing....";
    var req = makeRequest();
    req.open("GET", "writing.php?page=who");
    req.onreadystatechange = handlewhowritResp;
    req.send(null);
    
    setTimeout('whoiswriting()', 1500);
    }
    Then req would be local and new in each function.

    This all has primarily to do with what is known as scope in javascript and other programming languages. Making a separate version of everything is one way of dealing with the issue, but it is also usually the least efficient one.

    You might not be ready for this yet, but you may, so have a look here:

    http://www.dynamicdrive.com/forums/s...3&postcount=23

    It probably (with little or no modification) can handle all of your requests with little or no repetitious coding. It also is more robust, selecting the optimal request object for each browser, not just any that might be available, and covers more browsers. It also determines just once which request object that may be, instead of having to choose each time a request is made.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. The Following User Says Thank You to jscheuer1 For This Useful Post:

    clowes (12-16-2009)

  3. #12
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Absolutely brilliant John. Hit the nail on the head. Sorted the problem, and managed to explain it to a novice with little to no information. Thank you ever so much.

    I had a look at the post you linked, and although code wise it goes over the top of my head, I can see what it does, and will have a look at integrating it.

    My intial thoughts on a quick look are that

    new loadXmlHttp('requested_url', 'target_element_id')

    essentially loads the requested_url and puts the results in the div with id target_element_id?

    I then image you could do something like:

    PHP Code:
    checkwhoiswriting = new loadXmlHttp('requested_url''target_element_id');

    setTimeout(checkwhoiswriting,10000); 
    Is that right or have I missed the point
    Thanks again.

  4. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    That's very close, it would be more like so:

    Code:
    function checkwhoiswriting(){
    	new loadXmlHttp('requested_url', 'target_element_id');
    	setTimeout(checkwhoiswriting, 10000);
    } ;
    checkwhoiswriting();
    Though even that could be modularized instead of having to be repeated for each one. However, it's a good start. Before we worry about modularizing it further, we need to make sure we are passing the correct value of 'who' to the request. This (going by the earlier code you were showing):

    Code:
    function checkwhoiswriting(){
    	new loadXmlHttp('writing.php?page=who', 'state');
    	setTimeout(checkwhoiswriting, 10000);
    } ;
    checkwhoiswriting();
    is not going to do it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #14
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    John,

    Im not sure if your knowldege delves into PHP but I will assume so.

    In reading your response, your first code snippet I completely understood.

    Your second code snippet I assumed was an example, but you state it 'is not going to do it'

    Could you elaborate on why?

    Surely it gets the response from the respective part of the writing.php script, and returns it to the 'state' div?

  6. #15
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Let's put it this way, I'm much better at javascript than I am at PHP. But this is still really mostly a question of javascript (about the 'who') only extremely basic PHP is involved. The PHP on writing.php is probably a bit beyond me or at the limits of what I can easily understand. The fact that you are passing writing.php a GET query value via javascript and AJAX though is not. It's obvious, to me at least, that the literal 'who' will not contain the value you want to send.

    Now, it's not clear to me and I think you haven't shown/explained enough for me to know where the javascript value that should replace the literal 'who' will come from. But it has to come from somewhere. It can't always be the literal 'who', if it were, 'who' would always be writing and 'who' would be the only person who ever writes.

    It would need to be (very loosely now, but still all javascript) something like:

    Code:
    function checkwhoiswriting(){
    	var who = something that is the value as a string;
    	new loadXmlHttp('writing.php?page=' + who, 'state');
    	setTimeout(checkwhoiswriting, 10000);
    } ;
    checkwhoiswriting();
    Last edited by jscheuer1; 12-16-2009 at 10:01 PM. Reason: specificity
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #16
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Ah, I understand what you are saying.

    Basically the only reason it is writing.php?page=who as opposed to writing.php is because I want to be able to have multiple things in one single PHP script. The literal value 'who' for the variable page IS what I want, and does return the data I want each time.

    PHP Code:
    if($_GET[page]=='who'){
    //LOOKUP PEOPLE WHO ARE WRITING FROM DB
    }

    if(
    $_GET[page]=='add'){
    //INSERT SOMEONE INTO THE DATABASE

    So essentially when the logged in user is typing into the message box (keyPress) a JS function is triggered which queries writing.php?page=add. I.E when a user is typing they are added to the database.

    I then have another function checkwhoiswriting(); which gets a list of people already in the database.

    So although they both query the same script, the data they return is different BECAUSE of the ?page=
    Hope that makes sense, and that again I have understood what you are saying.

  8. #17
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    That actually makes sense, though as I imagined, it is at my limits of understanding as regards PHP. As long as it all works out, I'm not going to complain.

    When I get more time, and if you are interested, I'll look into making this process (on the javascript side) more modular. At the very least, the more efficient setInterval can probably be used. But I'm still not sure whether or not I have enough information for more than that. If we get that far and I don't, I'll ask. However, just using the code we have been talking about so far (with the loadXmlHttp function) is a big improvement if it works out for you. Any questions on that, feel free to ask.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #18
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot for all the help John.

    Bed time for me now, but Ill have a play in the morning.. and see what I get.

    Cheers

  10. #19
    Join Date
    Jul 2008
    Posts
    65
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    John,

    Finally had a chance to play with your function.


    Code:
    <script language="javascript" type="text/javascript">
    <!-- 
    function loadXmlHttp(url, id) {
     var f = this;
     if (loadXmlHttp.xmlHttp){
      f.xmlHttp = loadXmlHttp.xmlHttp();
      f.el = document.getElementById(id);
      f.xmlHttp.open("GET", url, true);
      f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
      f.xmlHttp.send(null);
     }
     else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
    }
    
    loadXmlHttp.xmlHttp = null; loadXmlHttp.re = /^http/.test(window.location.href);
    /*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
    /*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
    try {loadXmlHttp.ie = window.ActiveXObject}catch(e){};
    @end @*/
    if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re))
     loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
    else if (/(object)|(function)/.test(typeof createRequest))
     loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others
    else {
     loadXmlHttp.xmlHttp = null;
    // Internet Explorer 5 to 6, includes IE 7+ when local //
    /*@if(@_jscript_version >= 5)
    try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};}
     catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){}}
    @end @*/
    }
    
    loadXmlHttp.prototype.stateChanged = function(){
     if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
      this.el.innerHTML = this.xmlHttp.responseText;
    }
    </script>
    
    
    
    <body>
    <div id='ajaxDiv'>
    <form>
    <input type="submit" value="Who is online?" onClick="loadXmlHttp('onlinelist.php', 'ajaxDiv');return false;">
    </form>
    </div>
    </body>
    I may be being completely stupid, but the above I would have thought would be the simplest implementation of said function.

    onlinelist.php simple says 'Hello 123'

    On clicking the button, nothing happens.

    Have I lost it?

  11. #20
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Get rid of the highlighted (fine point, but it's invalid):

    Code:
    <script language="javascript" type="text/javascript">
    The real problem is you need to use new:

    Code:
    onclick="new loadXmlHttp('onlinelist.php', 'ajaxDiv');return false;"
    There could be something else, but that's probably it.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. The Following User Says Thank You to jscheuer1 For This Useful Post:

    clowes (12-18-2009)

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
  •