www.webdeveloper.com
Results 1 to 6 of 6

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

  1. #1
    Join Date
    Jul 2014
    Posts
    3

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

    Hi;
    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
    Posts
    3
    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
    Posts
    512
    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 01:48 PM.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,413
    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">
    </form>
    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.
    Code:
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  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?

    Code:
    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



Recent Articles