Google Answers Logo
View Question
 
Q: Javascript or PHP code to alter the form action depending on a button ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Javascript or PHP code to alter the form action depending on a button
Category: Computers > Programming
Asked by: toph-ga
List Price: $2.00
Posted: 03 Aug 2002 13:40 PDT
Expires: 02 Sep 2002 13:40 PDT
Question ID: 50250
I am working on a form that can be viewed at:
http://www.nrcpara.org/bookstore/testing/orderform.php
I need to be able to alter the action of the form depending on whether
the user selects to to pay by Credit Card (type=payment name=card) or
Money Order (type=payment name=check).
For example, if the Credit Card option is selected, it will go to one
URL, but if the Money order button is selected it will go to another.
Thanks,
Christopher
Answer  
Subject: Re: Javascript or PHP code to alter the form action depending on a button
Answered By: dms-ga on 03 Aug 2002 14:32 PDT
Rated:5 out of 5 stars
 
Hi Christopher,

A simple piece of javascript will solve your problem...

Add the code below to your page. It will work with both IE and
Netscape.

Add the this onclick event to your payment radio buttons:
  
  onclick="changePaymentType()" 

  <input type="radio" name="payment" onclick="changePaymentType()"
value="card" checked>
  <input type="radio" name="payment" onclick="changePaymentType()"
value="check">

Put this script block somewhere on your page (you'll need to replace
the url's with the correct one's):

<script language="javascript">

	function changePaymentType(){
	
		var myForm = document.forms.form1
	
		if (myForm.payment[0].checked){
			myForm.action = 'myUrl1.php'
		}else{
			myForm.action = 'myUrl2.php'
		}

	}

</script>


Good luck!

Daaf

Request for Answer Clarification by toph-ga on 05 Aug 2002 13:01 PDT
Thanks for the help Daaf- much appreciated. 
Two quick items of clarification- 
I changed my input buttons and added the code but am not sure what to
do with the code:
onclick="changePaymentType()" 
Does it go right before the input buttons?

Also, what do I change the form action to??

<form name="form1" method="get" action="??????">

Thanks so much!

Clarification of Answer by dms-ga on 06 Aug 2002 02:18 PDT
Hi Christopher,

The code:
onclick="changePaymentType()"
Goes inside the input elements like this:
<input type="radio" name="payment" onclick="changePaymentType()"
value="card" checked>
  <input type="radio" name="payment" onclick="changePaymentType()"
value="check">


You need to change the form action to the URL you want to use. Set it
to the "default" url for payment by Credit Card since that is default
selected.

in the script block you'll need to replace the values: 'myUrl1.php'
and 'myUrl2.php' with the url's for the credit card option and for the
money order option.

I hope this solves everything for you..

Grtz,

Daaf
toph-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

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