Google Answers Logo
View Question
 
Q: JAVASCRIPT :conditional cgi script calling on form submit ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: JAVASCRIPT :conditional cgi script calling on form submit
Category: Computers > Internet
Asked by: myq-ga
List Price: $10.00
Posted: 21 Aug 2005 06:51 PDT
Expires: 20 Sep 2005 06:51 PDT
Question ID: 558325
Is there a way  using javascript (or otherwise) one could have a form
such that a different script is called depending on some of the
variables used.
Just for example, if a person selects  'member' from a dropdown menu,,
the form calls perl script 1 and if the person selects 'guest', the
form calls php script 2.
Answer  
Subject: Re: JAVASCRIPT :conditional cgi script calling on form submit
Answered By: palitoy-ga on 21 Aug 2005 07:28 PDT
Rated:5 out of 5 stars
 
Hello myq-ga

Thank-you for your question.

This can be achieved by using the following code.

First of all you need the form on your page, it should be something like this:

<form name="myform" onSubmit="return submitForm(this.select.value)"
action="" method="post" >
<select name="select">
  <option value="member" selected="selected">Member</option>
  <option value="guest">Guest</option>
</select>
<input name="Submit" type="submit" />
</form>

Notice, the name of the form and the onSubmit parameter.  This calls
the submitForm javascript function when the form is submitted.  One
parameter, the value of the dropdown box, is submitted to the
javascript function when the form is submitted.

The javascript to redirect you to another script would be done like
this and should be placed in the <head> section of your page:

<script>
function submitForm (choice) {
if ( choice == "member" ) { document.myform.action = "member.html" }
else { document.myform.action = "guest.html" };
document.myform.submit();
};
</script>

Basically, this says if the value submitted (choice - which comes from
the dropdown box on the form) is equal to 1 then submit it to
"member.html" otherwise it goes to "guest.html".  Hopefully you should
be able to edit this easily for your own use.

If you have any questions on this subject please ask for clarification
and I will do my best to respond swiftly.

Request for Answer Clarification by myq-ga on 21 Aug 2005 08:48 PDT
Great! fast work!
which  browsers (main) and versions will this work on and which will it NOT work on?

Clarification of Answer by palitoy-ga on 21 Aug 2005 09:05 PDT
Hi myq-ga

This should work on any browser that has javascript enabled as nothing
really browser-specific is being performed.  It obviously will NOT
work on any browser that has javascript disabled.
myq-ga rated this answer:5 out of 5 stars
super!thanks! (maybe should take a look at my perl question as well)

Comments  
Subject: Re: JAVASCRIPT :conditional cgi script calling on form submit
From: gansos-ga on 30 Aug 2005 16:19 PDT
 
I have worked with a similar function before, and there IS a browser
specific bug with it, under certain circumstances. In Internet
Explorer (IE) for Windows (at least version 6+), if you have an html
form tag with the name of 'action', this function tries to set that
tag to whatever value the function states, instead of setting the
action property of the form object. Obviously, this does not work, and
results in weird failure. Here's an example:

<form name="myform" onSubmit="return submitForm(this.select.value)"
action="" method="post" >

<!--This is the offending code for IE-->
     <input type="hidden" name="action" value="process">
<!--End offending code-->

<select name="select">
  <option value="member" selected="selected">Member</option>
  <option value="guest">Guest</option>
</select>
<input name="Submit" type="submit" />
</form>

The function is identical to the one palitoy wrote.

As far as I can tell, there is no easy workaround for this. It sounds
like you probably won't be dealing with something this specific, but I
though you should know, since you asked the question.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy