Solved

Switch the 'action' field in an HTML form dynamically

Posted on 2013-01-24
4
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

737 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