views:

2637

answers:

6

Does anyone can tell me what is going wrong with this code? I tried to submit a form with javascript, but error ".submit is not a function" shown. See below for more details of the code:

<form action="product.php" method="get" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input onclick="submitAction()" id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">
function submitAction()
{
    document.frmProduct.submit();
}
</script>

I also tried this:

<script type="text/javascript">
function submitAction()
{
    document.forms["frmProduct"].submit();
}
</script>

both are show me the same error :(

A: 
<form action="product.php" method="post" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">

document.getElementById("submit_value").onclick = submitAction;

function submitAction()
{
    document.getElementById("frmProduct").submit();
    return false;
}
</script>

EDIT: I accidentally swapped the id's around

Chad Grant
document.getElementById("frmProduct").submit is not a function :(
Jin Yong
Do you have more than one element with the id 'frmProduct'?
tvanfosson
Adding submitAction as a handler for onsubmit and then invoking submit from it may create an infinite loop. The handler should be associated with onclick of the button.
tvanfosson
thanks for the catch tvan, I wrote it too fast.
Chad Grant
Downvoting accurate answers huh? lame guys, step on us to get those points you're selling your souls for
Chad Grant
A: 

Use a debugger to trace "document.frmProduct". I believe document.frmProduct will turn out to be an array of forms instead of a form. You should try:

  1. removing id attribute for the form but leave the name intact.
  2. check the generated html for instances of elements having id or name = "frmProduct"
Salman A
A: 

You can try

<form action="product.php" method="get" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input onclick="submitAction(this)" id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">
function submitAction(element)
{
    element.form.submit();
}
</script>

Don't you have more than one form with the same name ?

Fabien Ménager
A: 

I have to admit, my answer to you has to be "Remove the JavaScript".

Just use a submit button, which will work whether or not JavaScript works... and if you want to perform another action in JavaScript, use the onsubmit event in the form header...

<form ..... onsubmit="return someaction(this)">

You can then use "this" to do things (including this.submit() if you really must!)

<input type="submit" value="Submit">

Edit: I just made a quick edit to say, if you want to prevent the form from submitting, you need to return false from "someaction"

Sohnee
A: 

Use getElementById:

document.getElementById ('frmProduct').submit ()
Ilya Birman
+7  A: 

"submit is not a function" means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.

When you name the button submit, you override the submit() function on the form.

epascarello
There is no button named submit in the question code. So this is nonsense.
KooiInc