when i click on a submit button it should take me to get.php but it doesn't whats wrong?

<form enctype='multipart/form-data' action='' method='POST' name='form'>
<div id=$counter><input type='submit' name='webpage' value='Add Webpage' onClick='return changeAction1(this);' /></div>
</form>
<script type="text/javascript">
function changeAction1(form)
{
form.action = "get.php"
}
function changeAction2(form)
{
form.action = "insert9x.php"
}
function changeAction3(form)
{
form.action = "insert8x.php"
}
 </script>

Recommended Answers

All 6 Replies

onClick='return changeAction1(this);'

I think that should be onclick="changeAction1(this);return true" but that still isn't quite right: 'this' refers to the <input> element (for which no action= is defined).

First the this that you get from the onclick will refer to the input button and not the form so when you state form.action its not changing anything. Try assigning an id to the form and pass that to your function. Then use element selection to get the form and change the action. Second by assigning a return value to the onclick function you are essentially bypassing the submit event to the form. Also try setting the form action by using the setAttribute method rather than attempting to address it directly.
Example:

<form id="formId" enctype='multipart/form-data' action='' method='POST' name='form'>
<script type=text/javascript>
function handleFormSubmit(elId)
{
   var formEl = document.getElementById(elId);
   changeAction1(formEl);
}
function changeAction1(form)
{
form.setAttribute("action","get.php");
}
</script>
<div id=$counter>
   <input type='submit' name='webpage' value='Add Webpage' onClick='handleSubmit('formId');' /></div>
</form>

'this' refers to the <input> element (for which no action= is defined).

The following

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">
function changeAction1(x)
    {
    x.parentNode.parentNode.action = "get.php"
    }
    </script>
    <title></title>
  </head>
  <body>
    <form enctype='multipart/form-data' action='' method='post'
    name='form' id="form">
      <div id="$counter">
        <input type='submit' name='webpage' value='Add Webpage'
        onclick="changeAction1(this);return true">
      </div>
    </form>
  </body>
</html>

works as you intended.

works as you intended.

but is ugly as sin.

A more common (and graceful) approach is

function changeAction1(x)
    {
    x.action = "get.php"
    }
</script>
<form enctype='multipart/form-data' action='' method='post'
    name='form' id="form">
      <div id="$counter">
        <input type='submit' name='webpage' value='Add Webpage'
        onclick="changeAction1(this.form);return true">
      </div>
    </form>

Hi,
this is just another option:

<html>
  <head>
    <script type="text/javascript">
function changeAction1(frm)
    {
    	frm.action = "get.php";
    }
    </script>
    <title></title>
  </head>
  <body>
    <form enctype='multipart/form-data' action='' method='post'
    name='form' id="form" onsubmit="return changeAction1(this);">
      <div id="$counter">
        <input type='submit' name='webpage' value='Add Webpage'>
      </div>
    </form>
  </body>
</html>
Member Avatar for rajarajan2017

Short and Sweet code by essential, just to add more:

<html>
  <head>
    <script type="text/javascript">
function changeAction1(frm)
    {
	if (frm.id=="one")
	    	frm.action = "get.php";
	else if (frm.id=="two")
		frm.action = "post.php";
    }
    </script>
    <title></title>
  </head>
  <body>
    <form enctype='multipart/form-data' action='' method='post'
    name='form' id="one" onsubmit="return changeAction1(this);">
      <div id="$counter">
        <input type='submit' name='webpage' value='Add Webpage'>
      </div>
    </form>
  <form enctype='multipart/form-data' action='' method='post'
    name='form' id="two" onsubmit="return changeAction1(this);">
      <div id="$counter">
        <input type='submit' name='webpage' value='Add Webpage'>
      </div>
    </form>
  </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.