Link to home
Start Free TrialLog in
Avatar of eeyore7250
eeyore7250

asked on

Submit a Non-Named Variable to a URL via a GET

I need to get a varable into the URL after a submit of a GET. The problem is, I don't want this variable to be named. I have tried the following two methos without success:

1. <form method='get'><input type='hidden' value='abc'></form>
2. <form method='get'><input type='hidden' value='abc' name=''></form>

The reason I need to do this is it's submitting to a third party web application and the first input parameter for the web application is unnamed. So my URL should look something like this:
http://localhost/webapp?abc

Any ideas?
Avatar of Leviter
Leviter
Flag of Netherlands image

You can change the action of the form manually by using javascript.

Your form could look something like this:

<form name="myForm" method="get">
    <input type="hidden" name="myField">
    <input type="submit" onClick="document.myForm.action='webapp?' + document.myForm.myField.value;">
</form>
Avatar of eeyore7250
eeyore7250

ASKER

Thanks for your response, however it doesnt achieve the required result. This is the resulting URL of your test:
http://localhost/webapp?myField=abc

Its the same with a get as if I had specified the action myself like this (apart from I wouldn't have an unwanted "myField"variable in the URL):
<form method="get" action='http://localhost/webapp?abc'>

BTW: That would work if the methos was post!
Whoops....

I see my mistake... There still is the hidden field inside the form. When the form gets submitted... it also sends the field.

Is it possible to write the value of the field into a javascript variable?

<script language="JavaScript">
<!--
    var hiddenField = "abc";
//-->
</script>

<form name="myForm" method="get">
    <input type="submit" onClick="document.myForm.action='webapp?' + hiddenField;">
</form>

Otherwise you could move the hidden field into another form that you won't submit, but where you do get your value from.... Like so:

<form name="hiddenForm">
    <input type="hidden" name="myField">
</form>

<form name="myForm" method="get">
    <input type="submit" onClick="document.myForm.action='webapp?' + document.hiddenForm.myField.value; myForm.submit();">
</form>
Thanks again for your quick response. Your method is still adding the variable to the Action URL. Variables in the Action URL are removed when you submit a form via GET. They only stay if it is a POST.
eeyore7250,

This code does do the trick.... I checked it with the logfiles to see what is submitted... and you can clearly see it on the URL...

<html>
<body>
<script language="JavaScript">
<!--
    var hiddenField = "abc";
//-->
</script>

<form name="myForm">
    <input type="button" onClick="document.location.href='test.html?' + hiddenField;">
</form>

</body>
</html>


Ok, now that doesnt submit the other form variables. (Of which there could be any number of)
Yes, please delete
Avatar of Michel Plungjan
If you had told us you wanted more than one field PLUS the javascript var then we could have told you this in case of GET:


<form action ="http://www.thirdparty.com/someaction" "
onSubmit="myVar='abc';
loc = this.action+'?'+myVar;
for (i=0;i<this.elements.length;i++) {
  loc += '&'+this.elements[i].name+'='+escape(this.elements[i].value);
}
location=loc;
return false">
<input type="text" name="field1" value="">
<input type="text" name="field2" value="">
<input type="text" name="field3" value="">
<input type="text" name="field4" value="">
<input type="submit">
</form>
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial