onclick form submit (newby question)

Discussion in 'JavaScript' started by cyclopsvs, Aug 31, 2006.

  1. #1
    Hello Javascript users,

    I'm completly new in web development and can't figure out how i should do the following.

    I have a connection with a database and based on the data retrieved from the database the following links could be shown in a browser

    <a href='test1'>test1</a><br>
    <a href='test2'>test2</a><br>
    <a href='test3'>test3</a><br>

    Besides these links i have a variable i wanted to pass along as a hidden variable so what i did was i created a header and right after the header i created a form with a hidden variable like

    <input type='hidden' name='id' value='100'>

    so what i want to do is when someone selects one of the <a href> i want the user to go to the adress specified with the link but i also want to pass along the hidden variable.

    i thought that with onclick would be the best option but i can't figure out how i should encode that.

    any help or suggestions are appreciated,

    thanks in advance for your time,

    richard mendes
     
    cyclopsvs, Aug 31, 2006 IP
  2. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could add the onclick as you had mentioned, to your links:

    <a onclick="window.navigate(this.target + '?id=' + form1.id.value)" href='test1'>test1</a>

    where the id of your input box is 'id'.
     
    Darrin, Sep 1, 2006 IP
  3. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi Darrin - I thought your solution was interesting, but I couldn't get it to work. The browser would always go to test1 (from the href) and never be overridden by the onclick.

    Also, I *think* your solution will only work in IE - document.getElementById is supported by more browsers.

    <input type='hidden' id='Myid' value='100'>

    <a onclick="window.location='test1?id=' + document.getElementById('Myid').value" href='#null' >test1</a>

    Note that I have changed the hidden field, to use 'id' instead of 'name'
     
    BurgerKing, Sep 1, 2006 IP
  4. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So, I'm guess that your solution still does not work?

    It looks like you have it. The href of '#' or '#null' should not override the onclick event value. You could also return null from your onclick event to stop the link from happening.

    This looks like it should work.
    <input type='hidden' id='Myid' value='100'>

    <a onclick="window.location='test1.html?id=' + document.getElementById('Myid').value" href='#null' >test1</a>


    If you wanted to keep the HREF link as it was, you can do this:

    <input type='hidden' id='Myid' value='100'>

    <a onclick="window.location='test1.htm?id=' + document.getElementById('Myid').value;return false;" href='http://www.google.com' >Google</a>

    Sorry about the IE thing, it looked like your questions revovled more around accessing the value from your field than browser compatability syntax. I guess I shouldn't always assume everyone will realize this.
     
    Darrin, Sep 2, 2006 IP