Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    javascript query string

    Hi I am trying to create a query string similar to the one available here: http://andrewu.co.uk/tools/request/e.../example1.html

    The problem that I am having is that I have more than one button on my page and even though my id's are unique all of my data is displaying on the action page at the same time. The code that i am using for my buttons is:

    Code:
    <label for="name_1">Name:</label>
            <input type="text" name="name1" id="name_1" tabindex="1" size="40" value="Donald Duck" />
    
            <br />
            <br />
    
            <input type="submit" id='view1' value="Submit" tabindex="2" />
            <label for="name_1">Name:</label>
            <input type="text" name="name2" id="name_2" tabindex="1" size="40" value="Mickey Mouse" />
    
            <br />
            <br />
    
            <input type="submit" id='view2' value="View" tabindex="2" />
    I have a lot more than 2 buttons on my page. when a button is clicked I would like to be able to get the values from the query string and only see the values for the button that was clicked like this one:

    http://andrewu.co.uk/tools/request/e...ndrew+Urquhart

    but instead I am getting Name: Donald Duck, Name: Mickey Mouse. What would I have to add or change to obtain just one Name to display at a time but have it be different depending on which button is pressed?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Why not simply make each one a separate <form>??

    Code:
    <form method="get" action="nextPage.html">
        <input type="text" name="name" value="Donald Duck" />
        <input type="submit" value="Submit" />
    </form>
    <form method="get" action="nextPage.html">
        <input type="text" name="name" value="Mickey Mouse" />
        <input type="submit" value="Submit" />
    </form>
    Now you don't need *ANY* JavaScript AT ALL on this page. Presto.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    because i would like the correct name to be placed onto the page receiving the query string and not to make multiple pages. I would have 1000's of pages.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Take a look again.

    BOTH those <form>s have the *SAME* action=

    Meaning BOTH forms would go to the same ONE page.

    If you click on the first submit button, the resulting URL is
    Code:
        nextPage.html?name=Donald+Duck
    If you click on the second submit button, the URL is
    Code:
        nextPage.html?name=Mickey+Mouse
    Then, on the next page, you simply parse the query string to get the chosen name.

    EDIT: The + in the URLs is the result of the browser *correctly* encoding the space in the querystring. It's normal and expeced. It would also be legit to have %20 there instead of the +.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    how would that work for a table? I really want to place everything into separate rows

    Code:
    <tr >
    				
    					<td><input type="text" name="name" id="name" value="Donald Duck"  /></td>
    					<td>
    						<input type="button"  value="Show" title="Show"   onclick="showNextPage(' example.html')">
    					</td>
    				</tr>
    <tr >
    				
    					<td><input type="text" name="name" id="name" value="Mickey Mouse"  /></td>
    					<td>
    						<input type="button"  value="Show" title="Show"   onclick="showNextPage(' example.html')">
    					</td>
    				</tr>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    That's no problem.

    Just put the <form> before the <tr> of the row and the </form> after the </tr> of the row.

    CAUTION: That might not validate in XHTML, but I'll bet it works in every browser, nevertheless.

    **************

    However, if your code really is as simple as you show, you could do it like this:
    Code:
    <form>
    <table>
    ...
    <tr >
        <td><input type="text" name="n1" value="Donald Duck"  /></td>
        <td><input type="button"  value="Show" title="Show"  
                   onclick="location.href='example.html?name=' + escape(this.form.n1.value);" />
        </td>
    </tr>
    <tr >
        <td><input type="text" name="n2" value="Mickey Mouse"  /></td>
        <td><input type="button"  value="Show" title="Show"  
                   onclick="location.href='example.html?name=' + escape(this.form.n2.value);" />
        </td>
    </tr>
    ...
    </table>
    </form>
    And that's legal wherever.

    No other JS needed.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    if you added two more td's to the first row would it then become:

    Code:
    <tr >
        <td><input type="text" name="n1" value="Donald Duck"  /></td>
        <td><input type="text" spouse="n2" value="Daisy"  /></td>
        <td><input type="text" uncle="n3" value="Scrooge"  /></td>
        <td><input type="button"  value="Show" title="Show"  
                   onclick="location.href='example.html?name=' + spouse=' + uncle=' + escape(this.form.n1..n2.n3.value);" />
        </td>
    </tr>

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    No. You can't intermix like that.

    Has to be one at a time:
    Code:
    <tr >
        <td><input type="text" name="n1" value="Donald Duck"  /></td>
        <td><input type="text" name="n2" value="Daisy"  /></td>
        <td><input type="text" name="n3" value="Scrooge"  /></td>
        <td><input type="button"  value="Show" title="Show"  
                   onclick="location.href='example.html?name=' + escape(this.form.n1.value) 
                            + '&spouse=' + escape(this.form.n2.value) 
                            + "&uncle=' + escape(this.form.n3.value);" />
        </td>
    </tr>
    But now you'd have to use different name in each row. So the next row would be n4, n5, n6, etc. If you are doing this by hand, that's no big deal, of course. Or you could easily use PHP to generate it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    when i am on the receiving page will i still use

    Code:
    <script type="text/javascript">
    			
    			document.write("Name: " + Request.QueryString("name"));
    			
    		</script>
    to get the value from the query string to be where i want it or would it become

    Code:
    <script type="text/javascript">
    			<!--
    			document.write("Name: " + Request.QueryString("n1"));
    			
    		</script>

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    You use the name that matches what is in the query string. The name in the <form> is *IRRELEVANT* (unless you use multiple forms as I also suggested...but I can see why you'd avoid that in ASP.NET pages).

    You can easily find what to match by simply *looking* at the URL in the browser!

    If the URL is (example)
    Code:
    mypage.aspx?name=Donald+Duck&spouse=Daisy&uncle=Scrooge
    then your ASP.NET code will, indeed, need to use
    Code:
        Request.QueryString("name")
        Request.QueryString("spouse")
        Request.QueryString("uncle")
    Simple as that. Look at the query string. Write the code to match.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    goofball1980 (04-10-2011)

  • #11
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks it is working great

  • #12
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    On the receiving page how could I take the values for name and spouse and input them into a dropdown menu and input box?

    here is an example of what I have done so far:

    http://userpages.umbc.edu/~andrade1/...0-%20Copy.html

    using document.write works great for writing the value to the page but I don't understand how to use document.write with a drop down menu and input box to retrieve the value from the query string

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    If the receiving page is indeed an ASP.NET page, then you should be doing that in the C# code.

    You *can* do it in JS in HTML, but I can't see how that's appropriate for an ".aspx" receiving page.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Nov 2010
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    both of the pages are html

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Sorry, (a) I mixed you up with another poster and (b) you keep use Request.QueryString("xxx") which is ASP/ASP.NET coding. I wouldn't expect you to use that in HTML/JS coding.

    Anyway, you'll have to show an example of your second page. I don't understand how this relates to "document.write with a drop down menu".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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