views:

18

answers:

2

Hi. I'm new to Ajax. I'd like to populate a hidden field on a form with a responseText from the server. I'm able to display the responseText in the HTML as an innerHTML. I'm just unsure how to populate the hidden field on the form. Any suggestions will be greatly appreciated!

:)

Here's the JS:

  function getLocation(locationrouting) 
    {
   var getLocation= newXMLHttpRequest(); // sending request
   getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + locationrouting, false);
   getLocation.send(null); // getting location

     var dv = document.getElementById("location_div");
     var verifyUnfound()

     if (getlocation.responseText === 'LOCATION NOT FOUND')
     {
       dv.style.color = "red";
     }
      else 
     {
      dv.style.color = "black";
    }
   dv.innerHTML = getLocation.responseText;
    }
A: 

HTML:

<input type="hidden" id="someid" name="somename" value="somevalue">

JS:

var hiddenField = document.getElementById('someid');
hiddenField.value = <whatever>;

You could change your function to:

function getLocation(locationrouting) {
    var getLocation= newXMLHttpRequest(); // sending request
    getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + locationrouting, false);
    getLocation.send(null); // getting location

    var dv = document.getElementById("location_div");
    var verifyUnfound();
    var hiddenField = document.getElementById('someid');

    if (getlocation . responseText === 'LOCATION NOT FOUND') {
        dv.style.color = "red";
    } else {
        dv.style.color = "black";
    }
    dv.innerHTML = getLocation . responseText;
    hiddenField.value = getLocation . responseText;
}
NullUserException
Thanks for the reply! So given my JS, how should I modify your code to replace whatever?
Spockrates
@Spock Do you want to put the response there?
NullUserException
Would this work? var hiddenField = document.getElementById('someid');hiddenField.value = ('getLocation.responseText');
Spockrates
Yes, I do want to put it there. That is, I'd like the responseText displayed on the page to also be the value for the hidden field. This way, I can use my validation JS to prevent the form from submitting if the hidden field has that value. Thanks.
Spockrates
@Spock See my updated answer
NullUserException
Sweet! Easier than I thought. Thanks much. :)
Spockrates
A: 

Use jQuery, it'll make setting the value and doing the AJAX request easier.

$("#something").attr("value", myvalue)
Owen Allen
Thank you. I'd like the value to be the same as the getLocation.responseText. Please tell me how I should go about doing that.:)
Spockrates