开发者

passing Form value to a xmlhttpPost function [closed]

开发者 https://www.devze.com 2023-02-20 20:08 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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:

  1. You should declare variables with var

    var $tag = ... ;
    
  2. You should give your <input> an "id", and use "document.getElementById()" to locate the element:

    var $tag = document.getElementById('q');
    
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消