views:

25

answers:

2

I have a textbox with id kTb. When I click the button after entering some value in the textbox, the below changeV(str) is called inside a javascript ('onClick="changeV(kTb.value)"'). The 'alert(str)' gives me the value I entered in the textbox but when I pass it 'urll = "uSetENV.cgi?kullaTest=str"', I am NOT getting the entered value, its just str. Could you please let me know how to pass/get the entered value, kindly let me know if I am NOT clear, thanks.

==================================================================================

function changeV(str)

{

alert("starting change value");
alert(str);
urll = "uSetENV.cgi?kullaTest=str";
var reqq

//fw_setenv kullaTest str;
    if (window.XMLHttpRequest) {
    reqq = new XMLHttpRequest();
    reqq.open("GET", urll, true);
    reqq.setRequestHeader( "If-Modified-Since",
                "Sat, 1 Jan 2000 00:00:00 GMT" );
    reqq.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
    reqq = new ActiveXObject("Microsoft.XMLHTTP");
    if (reqq) {
        reqq.open("GET", urll, true);
            reqq.setRequestHeader( "If-Modified-Since",
                "Sat, 1 Jan 2000 00:00:00 GMT" );
        reqq.send();
    }
}
alert("done change value");

}

+1  A: 

Use jQuery - http://api.jquery.com/jQuery.post/

And use POST, not GET.

alert("starting change value");
alert(str);
$.post('uSetENV.cgi', {'kullaTest': str}, function(data) {
   str = data;
   alert("done change value");
   alert(str);
});

alert('Here str is still not changed because AJAX requests are asynchronous');
how
Why use POST? I see nothing in the question that suggests it is a non-idempotent operation.
David Dorward
The longer I spend on Stack Overflow, the more I appreciate that "use jQuery" really is an appropriate answer to most "how do I do X in JavaScript?" questions.
Sean M
Thanks 'how'. I am new to JS and I am sure your answer will help me in the long run but as of now, I am going with David Dorward's answer. Thanks again for your answer.
chella
@David Dorward: POST is better because you can be sure that it is not cached. No need to add Expire headers, better extensibility - you can add very large requests. Just because simple things tend to grow very quickly.
how
I like caching. It saves bandwidth and makes interfaces faster.
David Dorward
Yes, but when you call "uSetENV.cgi" and expect ENV to be set you may not be ok with cached response that ENV was set some hours ago if you want to set it again right now. So caching is great but should be used with great caution. If it was "getLastMonthTopPosts.cgi" I would like caching too.
how
A: 

JavaScript doesn't have a symbol to indicate a variable (i.e. foo is a variable and doesn't need to be named $foo), so you can't perform interpolation of variables in strings. You have to build your string explicitly.

You should also deal with characters that have special meaning in URIs using encodeURIComponent.

urll = "uSetENV.cgi?kullaTest=" + encodeURIComponent(str);
David Dorward
Many thanks David Dorward, it is working for me.
chella