Solved

Switch the 'action' field in an HTML form dynamically

Posted on 2013-01-24
4
364 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i test my code? 1 40
Decoding Special HTML Characters using Javascript? 2 29
JavaScript let vs var 5 27
JS does not refresh 6 21
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
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…

809 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