Matthew Colley
Matthew Colley

Reputation: 11446

Javascript Submit Back to Form

I am building a search by city function. Once you have typed in the search field, and if there are multiple cities, you are then presented with all the options. Each option is a link with javascript, which when clicked should input the value (zipcode) back into the form for processing. The error I am getting is this:

Uncaught TypeError: Cannot call method 'submit' of undefined

Here is my javascript (yep Im using smarty):

<a href= "#" onclick="document['myform'].submit({$row.Zipcode}); return false">{$row.City}, {$row.State} {$row.Zipcode}</a>

Here is my form:

<form action="mymethod.php" method="POST" id="myform">

As you can see I am trying to pass the zipcode back to the form for processing upon 'click' Appreciate any insight anyone cares to share

Upvotes: 0

Views: 2423

Answers (3)

Matt K
Matt K

Reputation: 6708

Name you form instead: <form action="mymethod.php" method="POST" name="myform">

Then call it using: document.forms["myform"]

Upvotes: 0

Etienne Dupuis
Etienne Dupuis

Reputation: 13796

onclick="javascript:$('#myform').submit({$row.Zipcode}); return false"

Upvotes: 0

Rob W
Rob W

Reputation: 349042

The document.forms collection has named elements, which refer to <form> elements with a corresponding name attribute.

To refer to your form, add name="myForm".

<form action="mymethod.php" method="POST" name="myform">

Instead of using a link, I strongly recommend to use a <input type="submit"> element, to not drive away users who have disabled JavsScript.

Upvotes: 1

Related Questions