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.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    posting a variable by clicking a hyperlink

    Hello,

    Is it possible to post a variable when clicking on a hyperlink? Preferably without a form.

    Can a variable be posted without a form?

    Thanks

  • #2
    New Coder
    Join Date
    Dec 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you really need to POST the variable then you should take a look at ajax and existing ajax frameworks as that allows you to do that. If its okay to submit the variable via GET as well you can simply set url paramters in the link though.

    Code:
    <a href="url?myvar=1234">HI CLICK MY LINK</a>
    if you really need to post it here is a quick example on how to do it. Again you prolly want to check out one of the many ajax frameworks out there.

    Code:
    function postvar() {
    /* creating the  xml http request object */
    
    if(window.XMLHttpRequest)
      req = new XMLHttpRequest();
    else {
      if(window.ActiveXObject) 
        req = new ActiveXObject('Msxml2.XMLHTTP');
      else
        req = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    /* sending a variable via post to an url */
    
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    req.send('myvar=1234');
    }
    Now just make sure this code gets executed when the link in question is clicked

    Code:
    <a onclick="postvar()">HI CLICK MY LINK</a>
    http://demo.vegui.org - vegUI AJAX framework
    http://www.landsofkazram.com - browser based graphical MMORPG


  •  

    Posting Permissions

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