Cfhttp Passing Post Variables Via A Link That Is Clicked?
Solution 1:
My original answer completely missed the mark, but I can't delete an accepted answer, so for the purposes of clarity, I'm changing this answer to agree with Billy's comment above.
CFHTTP is for making a server-side request to another website (as if you were visiting the site yourself), processing the results of that request, and storing them in a variable, which can then be used by you to perform additional tasks against (say, the parsing of a website's homepage for specific values).
What you are asking for appears to be a completely unrelated piece of functionality, in which:
- You have an HTML anchor (hyperlink) on your page.
- When a user clicks that link, you wish to pass 1 or more variables to a new page.
The simplest way to accomplish this is to use a hidden form, and have the hyperlink submit then form. This matches Billy's suggestion in the comments for your question:
<formname="myForm"method="post"action="pageToSendVarsTo.cfm"><inputtype="hidden"name="username"value="theuser" /><inputtype="hidden"name="id"value="42" /></form><ahref="#"onClick="document.myForm.submit();">the link to click</a>
Here, you have an HTML form with two hidden fields, username and id, which do not display on the page as they are hidden (but take note, can still be viewed if the user views the source of your page). Next, "the link to click" visibly appears as a hyperlink to the user. When clicked, JavaScript is used to simulate the firing of the form submission; it essentially behaves as if the form had a submit button and the user clicked that submit button. What that means for this example's form is that both the username and id values are then HTTP POSTed to your 'pageToSendVarsTo.cfm' page, where they are available in the FORM scope to continue processing.
Solution 2:
If you don't want to have to send the variables back to CF to do the POST, you could also do this with some JavaScript magic. For example:
<aclass="postLink"href="foo.cfm?var1=a&var2=b">Click Me</a>
Now, say you want to request foo.cfm as a POST, passing var1 and var2 as POST variables (assumes jQuery):
<script>
$(function () {
$(document).on('click', 'a.postLink', function (e) {
e.preventDefault();
var newForm = $("<form method='POST'></form>");
if ($(this).attr('href').indexOf('?') != -1)
{
newForm.attr("method", $(this).attr('href').substring(0, $(this).attr('href').indexOf('?')));
var post_args = $(this).attr('href').substring($(this).attr('href').indexOf('?')+1, $(this).attr('href').length).split('&');
for (var i in post_args)
{
var keyVal = post_args[i].split('=');
newForm.append($("<input>", { type: "hidden", name:keyVal[0], value:keyVal[1] }));
}
}
else
{
newForm.attr("method", $(this).attr('href'));
}
console.log(newForm);
newForm.submit();
});
});
</script>
This will stop the normal processing of all links tagged with the class "postLink", and instead of going to that page, will construct a new form object with the parameters parsed out of the link URL included as form parameters. Then it will submit that form using POST to the same base href that the script points at. Here's a working fiddle: http://jsfiddle.net/VGaPH/
Post a Comment for "Cfhttp Passing Post Variables Via A Link That Is Clicked?"