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
https://forums.phpfreaks.com/topic/157476-form-submit-getelementbyid/
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. :)

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.