Solved

Switch the 'action' field in an HTML form dynamically

Posted on 2013-01-24
4
357 Views
Last Modified: 2013-01-24
I am trying to change the  <form action..... on a form based on the value of a textbox. The textbox on the form is txtBreed, the form is called myForm.  Here is the script.

function OnSubmitForm(){
	if(document.txtBreed == "Beagle")
	{
	document.myForm.action ="result_beagle.php";
	}
	else  if
	(document.txtBreed == "Hound")
	{ document.myForm.action ="result_hound.php";
	}
	return true;
	}

Open in new window


<form name="myForm" onsubmit="return OnSubmitForm();">
<input type="submit" value="submit" name="submit">
</form>

Open in new window


For whatever reason its not setting any form at all, when I hit submit the current page reloads.
0
Comment
Question by:DS928
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 500 total points
ID: 38815679
it should be

function OnSubmitForm(){
      if(document.txtBreed.value == "Beagle")
      {
      document.myForm.action ="result_beagle.php";
      }
      else  if
      (document.txtBreed.value == "Hound")
      { document.myForm.action ="result_hound.php";
      }
      return true;
      }
0
 

Author Comment

by:DS928
ID: 38815712
Thank you.  The same thing happens.  The current page reloads.
0
 

Author Comment

by:DS928
ID: 38815765
This worked.
function OnSubmitForm(){
      if(document.myForm,txtBreed.value == "Beagle")
      {
      document.myForm.action ="result_beagle.php";
      }
      else  if
      (document.myForm.txtBreed.value == "Hound")
      { document.myForm.action ="result_hound.php";
      }
      return true;
      } 

Open in new window

added the form name in front of the text box name.
Thank you, so much!
0
 

Author Closing Comment

by:DS928
ID: 38815768
Pointed me in the right direct, just required one tweak.  Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now