View Full Version : posting a variable by clicking a hyperlink

Dec 24th, 2006, 12:32 AM

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

Can a variable be posted without a form?


Dec 24th, 2006, 03:45 AM
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.

<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.

function postvar() {
/* creating the xml http request object */

req = new XMLHttpRequest();
else {
req = new ActiveXObject('Msxml2.XMLHTTP');
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');

Now just make sure this code gets executed when the link in question is clicked

<a onclick="postvar()">HI CLICK MY LINK</a>