View Full Version : Passing value to another web page with url hyperlink.
svdorr 09-16-2005, 10:11 PM Okay, I have a simple question, or at least I hope simple. I am trying to pass data in a url hyperlink to a form field in another web page.
Example.
www.mysite.com/page1.htm
There is a link that I want to click
www.mysite.com/page2.htm?COMMENT=General
After I click that, I want the form field on page2.htm to contain the word General. The form field name is COMMENT. Can this be done using only HTML? On page2.htm, is there code in the form field name COMMET that needs to added to pull that value?
Any help would be greatly appreciated.
Dan L 09-16-2005, 10:57 PM AFAIK, it's only possible with PHP (some funky JS could probably do it, but I wouldn't know how.)
Just do..
<input type="text" name="comment" value="<?=$_GET['comment']; ?>" ?>
shoogbear63 09-16-2005, 11:18 PM If it's a comment type text that you're looking to pass to another document, you should look into the $_POST command rather than $_GET as often there are problems with long variables being passed with $_GET. This is in php of course. I think I remember passing variables from page to page somehow in javascript back in college, though I don't remember specifically how, shouldn't be hard to figure out (I very well could be wrong here and apologize if I am).
gogocode 09-16-2005, 11:56 PM You can probably do this with Javascript, something like this...
window.onload = function()
{
if(document.location.search.match(/COMMENT=([^&]*)/))
{
document.getElementById('formelement').value = unescape(RegExp.$1);
}
}
svdorr 09-17-2005, 12:30 AM Thanks for the help. I will try it out tomorrow and see if I can get it to work. If any other ideas come to mind, just post them here. Thanks.
svdorr 09-17-2005, 04:40 AM Originally posted by DanX
AFAIK, it's only possible with PHP (some funky JS could probably do it, but I wouldn't know how.)
Just do..
<input type="text" name="comment" value="<?=$_GET['comment']; ?>" ?>
DanX, when I try this, it just puts "<?=$_GET['comment']; ?>" ?> in the form field and the data that was passed. Is there anything else I have to do? Thanks.
tree-host 09-17-2005, 07:12 AM <script language="JavaScript">
function getArgs() {
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = unescape(value);
}
return args;
}
var args = getArgs();
if (args.COMMENT) COMMENT = args.COMMENT;
</script>
document.write(COMMENT);
Then where you have your form field, you can do a bit of javascript to set the value to COMMENT I dont have that code to hand, but shouldne be too hard to find.
That should work for you in Javascript, a little long, but it works :)
svdorr 09-17-2005, 06:28 PM Can anyone elaborate on DanX's solution. I am having difficulty getting it to work. His seems to be the simplest. Any suggestions as to other popular forums (geared towards web development) I could search for an answer? I have been googling all night and have not found a solution to work yet. Thanks.
Dan L 09-17-2005, 06:39 PM Whoops, the very last question mark should have been either a / or nothing at all.
Technically that should work, if not you can try
<?php echo $_GET['comment']; ?> instead.
Using POST would be better though.. change your form method to POST and use $_POST instead of $_GET, for security reasons :)
svdorr 09-17-2005, 07:03 PM DanX,
That did it. Thank you very much. It worked on the simple test form, now I just need to integrate it to the existing form. Thanks again, you have made a difficult project that much simpler.
|