...

View Full Version : Get Rid of QueryString When Posting Form



arpan_de
01-10-2007, 02:07 AM
Assume that I come to the following URL by clicking a link in, say, Home.html:

http://www.myserver.com/Page1.asp?root=yes

Note the querystring ?root=yes in the above URL.

The Form in Page1.asp has many buttons. When any of the buttons is clicked, the Form gets posted & after doing the needful, comes back to Page1.asp. Note that I am not using the <input type=submit> element; I am using the simple <input type=button> element.

All the buttons call different JavaScript functions when any of them is clicked. All these functions have the

document.forms[0].submit()

line which I am using to submit the Form when any button is clicked.

The problem is since I come to Page1.asp with the querystring root=yes already appended to it when I visit this page for the first time (by clicking a link in Home.html), whenever any button is clicked to post the Form, it retains the querystring i.e. after submitting the Form, I am taken back to

http://www.myserver.com/Page1.asp?root=yes

but I want to get rid of the querystring when a button is clicked & the Form gets submitted i.e. after the Form gets posted, I want the URL to be

http://www.myserver.com/Page1.asp

& not

http://www.myserver.com/Page1.asp?root=yes

which is what is happening currently.

Is there any way to get rid of the querystring when the Form gets posted?

I tried changing the document.forms[0].submit() line to document.forms[0].submit("Page1.asp") but that doesn't make any difference. The querystring still remains appended to the URL after any of the buttons is clicked to post the Form.

felgall
01-10-2007, 06:06 AM
What is the action on the form tag? That's what gets run when you submit a form. Maybe you need to update the action attribute using Javascript.

david_kw
01-10-2007, 06:50 AM
You could try a server side redirect if there is a query string. I think

print $q->redirect("Page1.asp");

is how it would be done in Perl with CGI.pm

Does

response.redirect("Page1.asp")

work in ASP?

I've never done it so this is just an educated guess. :)

david_kw

arpan_de
01-10-2007, 10:59 AM
Stephen, I haven't set any action on the Form tag. When I am using document.forms[0].submit() to submit the Form, what's the need to specify the action on the Form tag?

Also I have specified in my post that I am not using the <input type=submit> element to submit the Form; rather I am using <input type=button> to submit the Form. So, if I am not mistaken, setting the action on the Form tag will any how not make any difference, isn't it? Please correct me if I am wrong.

David, I would like to post the Form using document.forms[0].submit() only as my app basically relies heavily on JavaScript. I don't want to use any server-side redirection. I need to incorporate lots & pots of changes if I have to change the redirection process using ASP since the app had been coded by someone else. It's just that the original coder left & I have being given the task to finish it.

BTW, is there any way to change the method of a Form (from POST to GET or from GET to POST) using JavaScript? I tried using

document.forms[0].method='post'

but that doesn't make any difference. The querystring still remains appended to the URL. I added the above line just before the document.forms[0].submit() line.

I also tried

document.forms[0].action="Page1.asp'

but here too, the querystring remains appended to the URL.

Please help me out......I am looking out for a solution desperately.

david_kw
01-10-2007, 03:13 PM
My understanding is that all form submits in html are posts. It's not clear to me exactly why the query string gets appended to the URL in that case other than to reflect the state change.

Unfortunately I'm at the end of my knowledge of the subject as I'm not aware of anything in javascript that would let you change the current address. Good luck.

david_kw

liorean
01-10-2007, 09:38 PM
Stephen, I haven't set any action on the Form tag. When I am using document.forms[0].submit() to submit the Form, what's the need to specify the action on the Form tag?The action attribute isn't optional, it's required. It should always exist in the HTML source code of a form and contain the URI of the target form handler.
BTW, is there any way to change the method of a Form (from POST to GET or from GET to POST) using JavaScript? I tried using

document.forms[0].method='post'

but that doesn't make any difference. The querystring still remains appended to the URL.It shouldn't make any difference since the attribute should be case insensitive, but try using "POST" instead of "post". If that doesn't work, specify it in the HTML source instead.
My understanding is that all form submits in html are posts. It's not clear to me exactly why the query string gets appended to the URL in that case other than to reflect the state change.Nope. The default method used is GET. Only if the method is explicitly specified as POST will that method be used.

david_kw
01-10-2007, 10:21 PM
The default method used is GET. Only if the method is explicitly specified as POST will that method be used.

I stand corrected. Not that I really doubted but I put this together to help me understand. Maybe it'll help. (Google doesn't handle it when it is a post but the theory should be sound)



<html>
<head>
<title>Form Post Test</title>
<script type="text/javascript">
function postIt() {
document.f.method = "post";
document.f.submit();
}
function getIt() {
document.f.submit();
}
</script>
</head>
<body>
<div>
<form name="f" method="get" action="http://www.google.com/search">
<input type="text" name="q" />
<input type="hidden" name="hl" value="en" />
</form>
<button onclick="postIt();">Post it</button>
<button onclick="getIt();">Get it</button>
</div>
</body>
</html>


Note, you can see my form sets the method to "get" and the postIt overwrites that to a "post".

david_kw

arpan_de
01-11-2007, 01:47 AM
The action attribute isn't optional, it's required. It should always exist in the HTML source code of a form and contain the URI of the target form handler.

liorean, the action attribute isn't a must in the Form tag; it is optional. If the action is not specified, then the page posts to itself when the Form is submitted. Try out this simple HTML code:

<html>
<body>
<form method=post>
<input type=text name="txt">
<input type=submit value="SUBMIT">
</form>
</body>
</html>
Anyway, friends, I have taken care of the problem by adopting a simpler server-side approach.

Thanks to one & all for your inputs.

Regards,

Arpan

david_kw
01-11-2007, 02:07 AM
liorean is right from what I know. Action is required in a form tag for html 4.

http://www.w3.org/TR/html4/interact/forms.html#h-17.3

david_kw
01-11-2007, 02:12 AM
Hmm I can't edit for some reason so consider this part of the previous post. =P



action %URI; #REQUIRED -- server-side form handler --


david_kw

david_kw
01-11-2007, 02:35 AM
Of course from my experience it doesn't stop working. It just won't validate.

(I hate it when edit won't work =P)

liorean
01-11-2007, 03:57 AM
liorean, the action attribute isn't a must in the Form tag; it is optional. If the action is not specified, then the page posts to itself when the Form is submitted.That only works because your browser has nonstandard behaviour to cover for HTML programmer mistakes. That doesn't mean it works in every other browser on every other platform, so don't count on it. If the specification says something is REQUIRED, then it's a really bad idea to leave it out unless you're indifferent to whether it works or not.
Anyway, friends, I have taken care of the problem by adopting a simpler server-side approach.Nice to hear it's been solved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum