Solved

Switch the 'action' field in an HTML form dynamically

Posted on 2013-01-24
4
352 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
Comment Utility
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
Comment Utility
Thank you.  The same thing happens.  The current page reloads.
0
 

Author Comment

by:DS928
Comment Utility
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
Comment Utility
Pointed me in the right direct, just required one tweak.  Thank you!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now