Jump to content

Form Submit (getElementById)


anujgarg

Recommended Posts

I have two forms (one above and one below) with same functionalities. The purpose for both forms are to search the zip codes. I am using following code for the above form (that work fines):

 

<form method="post" id="locator_full_form" action="/postdata.php" onsubmit=" fn_val_postal(document.forms[0].elements['locator_full_form']);">

 

<div>

<input type="hidden" name="form_id_hidden_field" value="locator_full_form" />

</div>

 

<p>

Enter your 5-Digit U.S. ZIP code or the first 3 characters of your Canadian postal code:

</p>

 

<p>

<input id="id_input_typed_zip" type="text" name="typed_zip_field" />

 

<a href="javascript:void(0);" onclick="if ( fn_val_postal( document.forms[0].elements('typed_zip_field')) ) { fn_loc_search_zipcode(); } return false;"><img src="/images/btn_submit.jpg" alt="Submit" /></a>

</form>

 

 

And the code for the another form (that is creating problem):

 

<form method="post" id="locator_full_form" action="/postdata.php" onsubmit=" fn_val_postal(document.getElementById('locator_full_form').value);">

 

<div>

<input type="hidden" name="form_id_hidden_field" value="locator_full_form" />

</div>

 

<p>

Enter your 5-Digit U.S. ZIP code or the first 3 characters of your Canadian postal code:

</p>

 

<p>

<input id="id_input_typed_zip" type="text" name="typed_zip_field" />

 

<a href="javascript:void(0);" onclick="if ( fn_val_postal( document.getElementById('id_input_typed_zip').value) ) { fn_loc_search_zipcode(); } return false;"><img src="/images/btn_submit.jpg" alt="Submit" /></a>

</form>

 

The problem is that it shows the alert message all the time. In javascript functions, I have set the minimum limit for 5 digits and also, the text box should not be blank.

 

I hope I am phrasing my problem understandable.

 

Any help will be highly appreciated.

 

TIA

 

Anuj

Link to comment
Share on other sites

Please use


tags. It's in the Forum Guidelines. Click the link in my sig.

 

In you second form:

<form method="post" id="locator_full_form" action="/postdata.php" onsubmit=" fn_val_postal(document.getElementById('locator_full_form').value);">

A form doesn't have a value, so calling it is useless. Actually, the first code can be improved if you just put fn_val_postal(this) instead of using some long JavaScript code to get the form you're already on. :)

Link to comment
Share on other sites

Thanks for the quick response Ken2k7.

What is the difference/output in following statements:

-- document.getElementById('locator_full_form').elements('typed_zip_field')

-- document.forms[0].elements('typed_zip_field')

 

Can we use 'elements' with getElementById??

Will the above two statements return different outputs if I use anyone of them in my second code above  (in my question)??

 

TIA

Anuj

Link to comment
Share on other sites

I just noticed that I don't think clicking a link will submit a form, so the onsubmit function for the form may not work.

 

There's nothing wrong with using getElementById to get that. I'm just saying that if you're in a form, I don't see why you have a onsubmit and a onclick that calls similar functions. Again, I don't think the onsubmit function executes because a link doesn't really submits a form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.