PDA

View Full Version : Passing form data in URL using post method



Barat
01-08-2007, 12:51 PM
Hi all,

I am trying to pass data from a form field (to help adsense target ads) and I am running into a wall. I know it should not be that hard and there should be a script that can help.

Anyway, here's the scoop. Normal form, using the POST method. I just want to take a form field (which will be the name of a state) and add it into the URL when the user clicks on submit.

Anyone have any pointers or examples? Thank you in advance.

djr33
01-08-2007, 06:39 PM
I don't know/think there's a way to do half of a form as post and half as get.

But.... you could try javascript.

Maybe something like:
onSubmit="this.action=this.action+'?fieldname='+fieldname.value", though I'm not sure it that works or if it even could work.

jscheuer1
01-08-2007, 11:01 PM
I think that would work as long as the code was written correctly. It might have to trigger off of something other than onsubmit as well, in order for the change in the value of the action attribute to occur in time to be useful. If that were to be the case, perhaps the onchange event of the element whose value you wanted to pass along would work out. It would, of course be a javascript only solution though.

jscheuer1
01-08-2007, 11:10 PM
Works pretty well actually, with one minor addition(red):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
<form action="some.htm" method="post" onsubmit="this.action=this.action+'?bob='+this.bob.value">
<input type="text" name="bob"><input type="submit" value="GO">
</form>
</body>
</html>

mburt
01-08-2007, 11:37 PM
Actually, when referring to the element within that element you don't even have to use "this":

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
<form action="some.htm" method="post" onsubmit="action=action+'?bob='+bob.value">
<input type="text" name="bob"><input type="submit" value="GO">
</form>
</body>
</html>

jscheuer1
01-08-2007, 11:43 PM
Could conflict in certain situations and might not work so well in older browsers unless the this keyword is used. Your idea did work fine in the few modern browsers I just tested it in.

mburt
01-08-2007, 11:45 PM
It obviously won't work externally, say defining this as document, which will cause an error, so like I said; It only works within an object, but just to be safe, you might as well put the "this" in there.

djr33
01-08-2007, 11:52 PM
Ha, glad it works. Maybe I'm finally getting the hang of JS.

mburt
01-08-2007, 11:53 PM
Lol, were in opposite positions, I think I'm just starting to get the hang of PHP :)

djr33
01-08-2007, 11:55 PM
I'll trade you ;)
Off to AIM!
(If you have AIM... //checking profile...)

mburt
01-08-2007, 11:56 PM
Oh crap.. I don't have AIM. Wait, I'll get it !

djr33
01-09-2007, 12:03 AM
Found your MSN sn, but you appear offline.

I say we're hijacking the thread, but that's ok, because it's already solved. So we're just celebrating the victory.

mburt
01-09-2007, 12:06 AM
Yeah, I accidently have it set to offline - signing in - :)

djr33
01-09-2007, 12:10 AM
///conversation moves to MSN.
Now, the thread can be less off topic :p