View Full Version : How do I trigger a Form Action using Javascript?

Gary Williams
12-14-2002, 10:23 AM
Hi All,

I have a flatfile (text) database that I search using a form and submit button. I need to be able to replace/trigger the form search by clicking on an ordinary button, so how can I insert the following form data into a javascript function?


<FORM NAME="Fan Search" ACTION="http://www.domainname.co.uk/cgi/search.pl" METHOD=POST>

12-14-2002, 12:16 PM
very simply:


Notice that I put fanSearch, because Fan Search is not valid. You are not allowed a space in there! :) (You will have to change that in your HTML code)

::] krycek [::

Gary Williams
12-14-2002, 12:52 PM
Hi Krycek,

Thanks for the swift reply, it worked a treat using:


inside the tag of the link button

Now I'm trying to get clever and pass (parse?) one of the search variables to the form when I hit the submit link. I have tried a number of ways I can think of to achieve this but, guess what, no luck.

Why does this not work?

onClick=document.forms.FansSearch.submit(input type=hidden name=SEARCH value=^Odyssey$)



12-14-2002, 01:05 PM
OK Gary, I think we need to start with a couple of basics about forms ;)

When you submit a form, all the values of all the controls on that form are sent to the page specified in the form's action property.

So, all those values that you have in hidden inputs will be sent, e.g. empty=ok

In order to send that search variable, you are going to have to either create a search textbox on the form, and allow someone to type it in, or else submit it yourself by some other means depending on what you want to do.

If your value is coming from a select box, then the select box should be in your form, along with your hidden elements. When the form is submitted, the value of the select box is sent too, allowing your form handling script to know what item was selected.

In order to help you with that, I need to know what server-side language you are using to process the form (e.g. PHP, or if I am right and I saw your site somewhere else in the forum, ASP). If you don't want to process the form on the server then you will have to decide on another method.

Note that the hidden information you are sending is not neccessary. It is a potential security hole to send details of your database through a form - that should be done automatically on the server.

You should ideally ask on the ASP forum for help with that bit :)

::] krycek [::

Gary Williams
12-14-2002, 01:24 PM
Hi Krycek,

OK, I understand so far.

What I have got already is a search form for my database that uses combo boxes, etc, to set the value of the variables. So if I choose 'fans' I get all of them and if I choose 'fans' and 'brass' I get only the brass fans displayed. As each fan is displayed, its image is displayed also.

What I want to achieve is to place some images of the fans throughout the site and, when the image is clicked on, the database is searched for that specific item (without needing combo boxes, ext).

My search routine is a perl script.

Thanks for your help


12-14-2002, 01:41 PM
I think the best way to do this would be to send the variables through the URL.

For instance, instead of using a form at all, simply make the link on your images go to yoursite/yourcgi.cgi?yourvar=yourvalue&yourvar2=yourvalue2 etc.

Then, your cgi script will be able to access those variables from the URL (called GET variables) just as easily as from a form (called POST variables) with two advantages:

1. It will be easy for you to set up your links
2. People can bookmark their search

This is a method that is commonly used, just look at sites like Google etc. :)

I hope that helps!

::] krycek [::

Gary Williams
12-14-2002, 05:53 PM
Thanks Krycek - you advice worked perfectly!

I linked a graphic with:


and it worked like a charm. I've now got the hang of parsing variables.

Thanks again


12-14-2002, 06:10 PM
glad I could help :)

btw, what you have done is passing variables, not parsing... parsing is a bit different ;)

::] krycek [::