By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,356 Members | 2,752 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,356 IT Pros & Developers. It's quick & easy.

changing 'search' part of url on submit

P: n/a
hey all,

Ok, a related question to my previous one on data dumpers for
postscript.

In the process of putting a form together, I'm using a lot of
placeholder variables that I really don't care about in the submitted
action. I'd therefore
like to get rid of them by doing something like:

function my_function
{
document.form.action="/my/action";
document.form.search="?important=1&variables=1&onl y=1"
document.form.submit();
}

This doesn't seem to work, nor does making an '.action' with an
embedded search string (document.form.action =
"/my/action?my=1&variables=1") Javascript seems to merrily ignore
these statements and simply evaluate the submit call with the html
variables.

How do you modify this value?

thanks again,

Ed

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Lee
horos said:

hey all,

Ok, a related question to my previous one on data dumpers for
postscript.

In the process of putting a form together, I'm using a lot of
placeholder variables that I really don't care about in the submitted
action.
So why are they in the form? If you want variables, use variables.
I'd therefore
like to get rid of them by doing something like:

function my_function
{
document.form.action="/my/action";
document.form.search="?important=1&variables=1&onl y=1"
document.form.submit();
}

This doesn't seem to work, nor does making an '.action' with an
embedded search string (document.form.action =
"/my/action?my=1&variables=1") Javascript seems to merrily ignore
these statements and simply evaluate the submit call with the html
variables.

How do you modify this value?


I don't believe a form has a search attribute. URL's do.
You can either set the search attribute of the URL, and not submit
the form, or you can submit the form and let the browser set the search
attribute.

Jul 23 '05 #2

P: n/a
> So why are they in the form? If you want variables, use
variables.
My point is that I want to use javascript to set the variables for a
url that I'm submitting. If there is a URL object that I could use to
do my dirty work, by all means I'd love to use it. A syntax primer
would be nice.
I don't believe a form has a search attribute. URL's do.
You can either set the search attribute of the URL, and not
submit the form, or you can submit the form and let the
browser set the search attribute.


Fair enough, but again an example would be cool. Like I said, I'm
rather new at all this. All of this would be solved if there was a
pretty datastructure printer that could show you what you could and
could not set given a javascript object.

Venkman comes close, but annoyingly it doesn't let you search the
attributes of the variables that you print out in the
'local variables' window...

Ed

Jul 23 '05 #3

P: n/a
horos wrote:
So why are they in the form? If you want variables, use
variables.

My point is that I want to use javascript to set the variables for a
url that I'm submitting. If there is a URL object that I could use to
do my dirty work, by all means I'd love to use it. A syntax primer
would be nice.


This is frequently done by putting a generated string in a hidden
form element that gets submitted with the form. The issue is that
if the user has JavaScript disabled, the hidden element wont be
populated with your string and you have to parse the submitted
name/value pairs anyway.

Create your string with the onsubmit event as by then the user should
have finished making selections and entering data so less event
handling for you.

[...]
--
Rob
Jul 23 '05 #4

P: n/a
> Create your string with the onsubmit event as by then the user
should have finished making selections and entering data so
less event handling for you.

Right, is what you are saying is that can you trigger the javascript to
fire a totally different URL than the one that is specied by a submit
call? And can onsubmit respond to a javascript submit function?

ie: say I had the following javascript:

function download_submit()
{
document.form.myurl=
"http://www.whatever.com?variable=1&value=1";
document.form.submit();
}

function on_submit()
{
return(document.form.myurl);
}

and the form element was defined as:

<form name="myform" onsubmit="javascript:on_submit()">
... some element using download_submit()
...
</form>

Could you use this to make a call to document.form.myurl? ie: the
download function would call submit, which would in turn call on_submit
to return a url to the form which would then be displayed? Right now,
my testing is saying no, but then again I could be doing something
wrong.

Ed

Jul 23 '05 #5

P: n/a
horos wrote:
> Create your string with the onsubmit event as by then the user

should have finished making selections and entering data so
less event handling for you.


Right, is what you are saying is that can you trigger the javascript to
fire a totally different URL than the one that is specied by a submit
call? And can onsubmit respond to a javascript submit function?

ie: say I had the following javascript:

function download_submit()
{
document.form.myurl=
"http://www.whatever.com?variable=1&value=1";
document.form.submit();
}

function on_submit()
{
return(document.form.myurl);
}

and the form element was defined as:

<form name="myform" onsubmit="javascript:on_submit()">
... some element using download_submit()
...
</form>

Could you use this to make a call to document.form.myurl? ie: the
download function would call submit, which would in turn call on_submit
to return a url to the form which would then be displayed? Right now,
my testing is saying no, but then again I could be doing something
wrong.


If you want to navigate to a page other than the one in the form's
action attribute, why are you using a form at all? Simply build the
string you want appended, create your URL, then change the
document.location and voila!
Jul 23 '05 #6

P: n/a
horos wrote:
> Create your string with the onsubmit event as by then the user
should have finished making selections and entering data so
less event handling for you.


Right, is what you are saying is that can you trigger the javascript to
fire a totally different URL than the one that is specied by a submit
call?


No, but you could if you wanted to. A form provides functionality to
send data to the server without using any JavaScript. You should
allow for the case where your script does not execute and the form is
just submitted. Presumably you are just using JavaScript to make the
submission more efficient for those users that have it available.

So the tactic is to get the form to submit the 'URL' that you want,
an example is below. When 'submit' is clicked, the onsubmit event is
fired. It gets the value from the visible field, adds it to the
hidden field (with a trivial modification), enables the hidden field,
disables the visible field, then returns 'true' so that the form
submits itself with the value of the hidden field in the 'URL'.

You can process the fields and build up whatever string you like in
the hidden field, then just let the form submit it.

I also do some trivial validation - if the text element is empty, an
alert is triggered, the function returns 'false' to the onsubmit
handler, which returns false to the form and so it doesn't submit.

If JavaScript is disabled, the form will just submit with the value
in the visible text field - the hidden field is disabled by default
so it won't be submitted. This way your server knows what it's
getting.

And can onsubmit respond to a javascript submit function?
Yes.
ie: say I had the following javascript:

function download_submit()
{
document.form.myurl=
"http://www.whatever.com?variable=1&value=1";
This is probably not doing what you think it should. :-)

'document.form' references the form named "form". The next bit
after the dot should match an element name in the form - it is
equivalent to:

document.forms['form'].elements['myurl']

Do you have an element called 'myurl'? If not, you are creating a
'myurl' property of the form and setting its value to "http://w..."

If you want to set a property of the element, such as its value:

document.form.myurl.value = 'some value';

Presuming that an element named 'myurl' exists in the form called
'form' and it has a value property that can be set.

A form does not have a 'url' (or 'URL') property:

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
document.form.submit();
}
Bzzzzt. You have onsubmit calling a function that itself calls
submit(), which will fire the onsubmit, which calls the function,
which calls submit()... you are in an endless loop. Have your
function return 'true' and the form will submit itself.

function on_submit()
{
return(document.form.myurl);
I guess this will return something to the onsubmit handler, which
may evaluate it to true (or something other than 'false') and submit
the form ... or not.
}

and the form element was defined as:

<form name="myform" onsubmit="javascript:on_submit()">
No need for the 'javascript:' pseudoprotocol. JavaScript is assumed,
unless you've used some other script type earlier (say VBscript).
... some element using download_submit()
...
</form>

Could you use this to make a call to document.form.myurl? ie: the
download function would call submit, which would in turn call on_submit
to return a url to the form which would then be displayed? Right now,
my testing is saying no, but then again I could be doing something
wrong.


This appears a bit muddled, have a look at the example below.

<script type="text/javascript">

function doSubmit(f){
var t = f.elements['tField'];

// Do some validation
if ( '' == t.value ){
alert('Please put something into the text field');
if (t.focus) t.focus();

// Stop form submitting if test failed
return false;
}

f.elements['hField'].value = 'horos\'s custom field'
+ f.elements['tField'].value;

// Disable visible field
f.elements['tField'].disabled = true;

// Enable hidden field
f.elements['hField'].disabled = false;

// Let form submit
return true;
}
</script>

<body>
<form action="" onsubmit="return doSubmit(this);">
<input type="hidden" name="hField" disabled>
<input type="text" name="tField" value="some text">
<br>
<input type="submit" value="Submit form">
<input type="reset">
</form>


--
Rob
Jul 23 '05 #7

P: n/a
> If you want to navigate to a page other than the one in the form's
action attribute, why are you using a form at all? Simply build the
string you want appended, create your URL, then change the
document.location and voila!


thanks...

that's the answer I was looking for.

Just a few points from all this for those who search later:

1) you can't use a document.form.submit() to trigger an onsubmit
tag (for some stupid reason)

2) to set any input you need to toggle the .value attribute of that
input, not the input itself (ie: document.form.attrib.value = x, not
document.form.attrib=x. You have to add a hell of a lot of frickin'
'value's ont the end of things to get stuff done imo) Of course there
seem to be some exceptions - document.location and document.form.action
being two - but this makes things even more confusing.

3) there is a command dd(), that is bundled with Venkman (the
javascript debugger) which allows for data dumping of forms and other
javascript objects.

Anyways, so far, it seems that javascript is a bit clunky for my
tastes... things don't seem to do what the user wants smoothly -- eg,
there's no reason why you *shouldn't* be able to customize a form to go
to anywhere you want with whatever search string you desire, likewise
data structure dumping *should* be built in..

Perhaps it'll get better as time goes on, but right now I feel that
I'm fighting the language to get done what I need to get done. I never
felt that way with perl, python, or C (java is another matter
though)...

Anyways, thanks much,

Ed

Jul 23 '05 #8

P: n/a
horos wrote:
If you want to navigate to a page other than the one in the form's
action attribute, why are you using a form at all? Simply build the
string you want appended, create your URL, then change the
document.location and voila!

thanks...

that's the answer I was looking for.

Just a few points from all this for those who search later:

1) you can't use a document.form.submit() to trigger an onsubmit
tag (for some stupid reason)


You can execute a form's onsubmit event two ways:

1. Call the submit buttons' click handler (supposing we named it
"submitA" in a form named "formA"):

document.forms['formA'].elements['submitA'].click();

2. Call it directly:

document.forms['formA'].onsubmit();

Note: this will not submit the form, if that is to happen, then:

if ( false != document.forms['formA'].onsubmit() ) {
document.forms['formA'].submit();
}

will do it onsubmit() returns something other than 'false'.

2) to set any input you need to toggle the .value attribute of that
input, not the input itself (ie: document.form.attrib.value = x, not
document.form.attrib=x. You have to add a hell of a lot of frickin'
'value's ont the end of things to get stuff done imo)

Of course. document.form.element is an element, not an attribute,
you can't 'set' it. You *can* set the element's attributes or
properties, one of which is the value. It likely also has name, id,
style and other attributes/properties that can be set. All are
specified in the HTML specification.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-FORM>
Of course there
seem to be some exceptions - document.location and document.form.action
being two - but this makes things even more confusing.
'location' is an attribute of the 'document' object and is
supported as "level 0", that is, it is legacy from before the first
W3C specification. It is supposed to be only a getter, but it seems
to operate the same as window.location. Others here know far more
about this than I do.
'action' is a property of the form object, just like 'value' is a
property of a form element.

What you are really complaining about is not being able to
distinguish between objects, methods, properties, etc. This is
common to all 'dot notation' languages - you either have to know the
correct syntax, look it up or work it out.

A good start is the element and attribute appendices to the HTML
specification:

<URL:http://www.w3.org/TR/html4/index/elements.html>

<URL:http://www.w3.org/TR/html4/index/attributes.html>

3) there is a command dd(), that is bundled with Venkman (the
javascript debugger) which allows for data dumping of forms and other
javascript objects.

Anyways, so far, it seems that javascript is a bit clunky for my
tastes... things don't seem to do what the user wants smoothly -- eg,
there's no reason why you *shouldn't* be able to customize a form to go
to anywhere you want with whatever search string you desire,
You can.
likewise
data structure dumping *should* be built in..

Perhaps it'll get better as time goes on, but right now I feel that
I'm fighting the language to get done what I need to get done. I never
felt that way with perl, python, or C (java is another matter
though)...

Anyways, thanks much,

Ed

--
Rob
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.