Results 1 to 6 of 6

Thread: [RESOLVED] How to catch form witch have the submit event ?

  1. #1
    Join Date
    Jul 2014

    resolved [RESOLVED] How to catch form witch have the submit event ?

    Suppose i have a page with many forms, and i don't know eather theirs names or identifiers. How to catch the form that the user have submitted to add some inputs to it before send its post data to server ?

    Please, tell how to do that with pure Javascript, no jQuery.
    Thank you

  2. #2
    Why would you be adding inputs during the submit? That doesn't even make sense since you can't trust client-side generated content in the first place -- can't you just do that server-side where it belongs?

    Reeks of "JS for nothing and your scripts for free... that ain't working, that's not how you do it."

    I could be wrong on that since you really havenae described your actual intent and purpose of this, but it really sounds like doing something in JS you really shouldn't be doing.

  3. #3
    Join Date
    Jul 2014
    People already do add input to forms using Javascript when they know the form id or form name. I don't know how to do that when i don't know the form id or name, there is one possibility is to catch the form witch have the submit event, anyone tell me how to catch the form witch have the submit event that is all.

  4. #4
    Join Date
    Mar 2009
    Add an "onsubmit" function to your form(s).

    And I can't figure out why you would need to do that either unless you want to include some data stored in localStorage with the form information.

    P.S. If you really do want nothing more than to know which form was submitted just include an input of type "hidden" in each form indicating which one it is. You don't need any javascript to do that.
    Last edited by Tcobb; 08-01-2014 at 12:48 PM.

  5. #5
    Join Date
    Mar 2007
    I understand why you possible would need to add fields to your form, it might be better to either have a text area that is hidden that is updated with the data you want to convey to the the server or to have hidden input fields that get updated at time of submission.

    Using 'id's' to hook the inputs IMHO is not the way to do this, what would be better is to use the DOM method of referencing and with your form submission you can pass a reference of the form to the function that makes it simpler to access those form elements.

    HTML Code:
    <form action="postaddress.php" method="post" onsubmit="return validate(this);" />
    <input name="one" type="hidden" value="">
    <input name="two" type="hidden" value="">
    <input name="user" type="text" value="">
    <input name="email" type="text" value="">
    <input name="submit" type="submit" value="Submit">
    Where it says onsubmit="return validate(this);" the function called validate(this) is having the form object passed to it via the 'this' object that refers directly to the form.
    function validate(o){
        o.one.value = new Date().getTime(); // get a time stamp
        o.two.value = md5( o.user.value + " " + o.email.value );
    return true;
    So... when you access the object 'o' in the validate function, you can hook the form fields you want to access more quicker. The example is overly simple but you should get the idea on how to move on.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Quote Originally Posted by Dareros View Post
    People already do add input to forms using Javascript when they know the form id or form name. I don't know how to do that when i don't know the form id or name, there is one possibility is to catch the form witch have the submit event, anyone tell me how to catch the form witch have the submit event that is all.
    Ok, I don't think you're wording what you are asking very well... You're not trying to trap submit, you're trying to figure out which forms have javascript submit events on them to target the FORM (not the event) for the addition of INPUT, right?

    var formList = document.getElementsByTagName('form');
    for (var t = 0; t < formList.length; t++) {
    	if (typeof formList[t].onsubmit == 'function') {
    		var input = formList[t].appendChild(document.createElement('input'));
    		// do whatever else you want to do to 'input' here.
    Should theoretically work if it was assigned with element.onsubmit = or the onsubmit attribute. I have no clue if it will work if the method was assigned with element.addEventListener or element.attachEvent.

    That's what you're thinking though, right? You want to add the input to the form if the form HAS an onsubmit handler. I think I and everyone else here was thinking you meant to add the input WHEN the submit handler was called -- something different entirely.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13381 seconds
  • Memory Usage 2,901KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates