www.webdeveloper.com
Results 1 to 5 of 5

Thread: AJAX/JavaScript Question

  1. #1
    Join Date
    Apr 2009
    Posts
    346

    AJAX/JavaScript Question

    I've been using a standard AJAX/JavaScript script for a while but the limitation it has is that the result can only be applied to one id. I'm just curious how it can output the result to two or more different places on the same page?

    Here's the script I've been using:

    Code:
    function get_price(){
        var hr = new XMLHttpRequest();
        var url = "Scripts/get_style_price.php";
        var fn = document.getElementById("style").value;
        var ln = document.getElementById("quantity").value;
        var vars = "style="+fn+"&quantity="+ln;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
        hr.onreadystatechange = function() {
    	    if(hr.readyState == 4 && hr.status == 200) {
    		    var return_data = hr.responseText;
    			document.getElementById("price").innerHTML = return_data;
    	    }
        }
        
        hr.send(vars); 
        document.getElementById("price").innerHTML = "loading...";
    }
    With the code above, the output is sent to the id of "price". I was wondering how to output it to two different id's so I can display it in two different places on the webpage.

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    I did something a few years ago where I used a server-side scripting language (ColdFusion) in conjunction with AJaX and JavaScript. It was a form that could have anywhere from 1 to 1000 rows of form fields, and in one particular field when a six-digit number was entered and then the field blurred it would call a function that would access another CF page that had a query that would search the database for whatever text was associated with that number and automatically populate another field in the same row. I passed the field name that needed to be populated with the text as part of the function call, and set it for asynchronous, and had the field name returned with the query so it always knew which field to populate. Can you do something similar?
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Apr 2009
    Posts
    346
    I think I know what you mean but I don't know how I could apply it.

    I'm just thinking in terms of a shopping cart, if someone enters a promo code it'll show the savings amount below the input field for the promo code, and then off in the right sidebar it'll automatically update the cart with that discount amount. So it'll apply that amount in two locations.

  4. #4
    Join Date
    Mar 2009
    Posts
    501
    If that is all you want to do, just add another line under your current:

    Code:
    document.getElementById("price").innerHTML = return_data;
    Code:
    document.getElementById("OTHERprice").innerHTML = return_data;
    Or did I misunderstand the problem?

  5. #5
    Join Date
    May 2012
    Posts
    1
    hello

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles