HI
How do i pass a value from a form to an Ajax function which will use that value to build a URL
<FORM id="form" METHOD="GET" ACTION="">
<b> Enter argument: </b>
<input size="40" name="q" value="">
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
function xmlhttpPost(){
$tag = q.va开发者_高级运维lue
$search = 'http://...&tag=$tag'
$search = encodeURIComponent($search)
request = new ajaxRequest()
request.open("GET","xmlget.php?url=" + $search, true)
...
thanks
Several things:
You should declare variables with
var
var $tag = ... ;
You should give your
<input>
an "id", and use "document.getElementById()" to locate the element:var $tag = document.getElementById('q');
The value should be appended to the string, and only that should be passed to "encodeURIComponent":
var $search = 'http://...&tag=' + encodeURIComponent($tag);
edit — A wise commenter points out that the apparent use o the URL in your sample code is such that you probably would want to encode the whole thing. That is, if what you want to do is pass a URL string entirely as a parameter value via another URL, then yes you would pass the whole thing to "encodeURIComponent". The point is that you have to make sure you encode the bits that should not be interpreted by the browser and server as part of URL syntax. Therefore, point 3 above may not apply (well it does, but it applies to the final URL, not the URL that's going to end up as a parameter).
I can partially answer your question. To get the value from the input control use:
document.getElementById("id").value
http://www.w3schools.com/jsref/met_doc_getelementbyid.asp
Essentially, if I've read this right, you're just asking "how do I get the value out of a textbox in JavaScript?". If so, then this should do the trick:
<FORM id="form" METHOD="GET" ACTION="">
<b> Enter argument: </b>
<input size="40" name="q" id="q" value="">
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
function xmlhttpPost(){
var tag = document.getElementById('q').value;
var search = 'http://...&tag=' + tag;
search = encodeURIComponent(search)
request = new ajaxRequest()
request.open("GET","xmlget.php?url=" + search, true)
Note, the id attribute on the input.
精彩评论