...

View Full Version : posting a variable by clicking a hyperlink



alex57
12-24-2006, 12:32 AM
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

vegu
12-24-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 */

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


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum