+ Reply to Thread
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By misson

Thread: Calling AJAX function relative to recordset ID's

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Calling AJAX function relative to recordset ID's

    Hope you can help.

    I have an AJAX script as below...

    Code:
    var ajaxDownrate;//create ajax variable
        
        //main funtion to create XMLhttp request from event call
        function ajaxDownrateCall(str)//str is value from field
        {
            //try various browsers
            try{
            // Opera 8.0+, Firefox, Safari
            ajaxDownrate = new XMLHttpRequest();
            } catch (e){
                // Internet Explorer Browsers
                try{
                    ajaxDownrate = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try{
                        ajaxDownrate = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e){
                        // Something went wrong
                        alert("Your browser does not support HTTP requests.");
                        return false;
                    }
                }
            }
            
            //specify url to call + variable which will be called using GET
            var url="../ajax/ajax_fail.php";
            url=url+"?downlink="+str;//add value and assign to link
            
            
            ajaxDownrate.onreadystatechange=downStateChanged;//function to receive data from server
            ajaxDownrate.open("GET",url,true);
            ajaxDownrate.send(null);
        }
        
        function downStateChanged()
        {
            if (ajaxDownrate.readyState==4)//check if response is ready from server
            {
                var result = ajaxDownrate.responseText;//retreive data from server
                //document.getElementById("AjaxOutput").value=result;//specify output in field
                document.getElementById("AjaxOutput").innerHTML = result;//specify output in div
            }
        }
    Then, in a loop from a recordset, I have links as below (also to display returned value)...

    HTML Code:
    <div class="contentbox" id="AjaxOutput">
                        
                        <div class="link"><a href="javascript:ajaxDownrateCall(<?php echo $row_fulltext_images['ID'];?>)">Fail</a></div>
                        <div class="box"><?php echo $row_fulltext_images['ID'];?><?php //echo $row_fulltext_images['WIDTH']; ?>x<?php //echo $row_fulltext_images['HEIGHT']; ?></div>
    </div>
    The link calls the AJAX and passes the ID from that loop to a php page as below...

    PHP Code:
    require_once('../Connections/discountdomains.php');
    include(
    '../includes/function_sanitise_sql_string.php');
      
    mysql_select_db($database_discountdomains$discountdomains) or die('Could not select db. ');

    $ajaxdownlink=$_GET["downlink"];
    $ajaxdownlink=GetSQLValueString($ajaxdownlink,"int");

    echo 
    $ajaxdownlink;

    $updatequery "UPDATE images SET SUIT = -10, THUMBOK = 0 WHERE ID = '".$ajaxdownlink."' LIMIT 1";
    $updateresult mysql_query($updatequery) or die('Could not update.');

    //clear thumbnail
    @unlink('../thumbs/'.$ajaxdownlink.'.jpg'
    With me so far???

    OK... here's the hitch..

    This only works once with the first link I click, regardless of how many rows are in the recordset. Also the innerHTML "AjaxOutput" always returns to the first iteration.

    Do I somehow have to create multiple AJAX functions for each row and if so, how can I do this in a loop?

    Hope this makes sense.

    Rich

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Calling AJAX function relative to recordset ID's

    Since you've only got one XHR object, only one AJAX request can be active at any one time. This is one reason why globals are bad. Move the declaration of ajaxDownrate into the definition of ajaxDownrateCall so that each invocation gets its own XHR. Either do the same with downStateChanged, or use this rather than ajaxDownrate within downStateChanged.
    Last edited by misson; 07-10-2011 at 12:32 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Calling AJAX function relative to recordset ID's

    Cheers Misson

    As suggested, I've moved the ajaxDownrate variable into the definition of ajaxDownrateCall - which works a treat.

    I'm not so clear on what you mean with the downStateChanged.

    I can't re-declare the ajaxDownrate variable within this function (or can I?)... which is what I thought you meant by "Either do the same with downStateChanged".

    Can I put the downStateChanged function within the ajaxDownrateCall function?

    Either way, the backend php works great - I'm just not pulling in the result.

    Rich

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Calling AJAX function relative to recordset ID's

    If you declare a variable within a function when a global variable with the same name is also declared, the local variable hides the global within the function. You can still access the global as a property of the global object, which is window in browsers (not that you'd need to in this case).

    As stated in my previous post, yes, you can put downStateChanged in ajaxDownrateCall:
    Code:
    function ajaxDownrateCall(str) {
        var ajaxDownrate;
        function downStateChanged() {
            ...
        }
        ...
    }
    Or use this rather than ajaxDownrate:
    Code:
    function downStateChanged() {
        if (this.readyState==4) {
            var result = this.responseText;//retreive data from server
            document.getElementById("AjaxOutput").innerHTML = result;//specify output in div
        }
    }
    Or both.
    Last edited by misson; 07-10-2011 at 05:50 AM.
    learning_brain likes this.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Calling AJAX function relative to recordset ID's

    Perfect.

    I just added the "str" variable to the output string.

    Code:
    document.getElementById("AjaxOutput"+str).innerHTML = result;
    ..followed by the unique linked div so that the two match.

    HTML Code:
    <div class="contentbox" id="AjaxOutput<?php echo $row_fulltext_images['ID'];?>">
    
    <a href="javascript:ajaxDownrateCall(<?php echo $row_fulltext_images['ID'];?>)">Fail</a>
    
    blah de blah
    
    </div>
    Works a treat - many thanks
    Last edited by learning_brain; 07-10-2011 at 02:38 PM.

+ Reply to Thread

Similar Threads

  1. calling php
    By daniel.norris656 in forum Free Hosting
    Replies: 4
    Last Post: 05-22-2011, 07:10 AM
  2. calling all wow players
    By Brandon in forum Gamer's Lounge
    Replies: 5
    Last Post: 10-27-2010, 08:14 PM
  3. Child's head crushed by obese relative.
    By Soki in forum Off Topic
    Replies: 6
    Last Post: 04-02-2008, 10:12 AM
  4. Calling for help!
    By ibnuasad in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 07-16-2006, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers