+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: form submission differences?

  1. #1
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    form submission differences?

    long story short, I ran into a weird difference between input type submit and using the javascript form submit function. when I use the regular input element to submit, post data goes through fine, but not when I use the javascript function. I'm usin the javascript for form validation so I can't really just drop that. any thoughts?


    Code:
    <input type="button" name="trackSubmit" value="Add Song" onclick="javascript:submitTrackForm()"/>
    vs
    <input type="submit" name="Submit" value="Add Song"/>
    with
    Code:
    function submitTrackForm() {
      var trackForm = document.forms["trackManager"];
    
      if(validateForm(trackForm)) {
        lockForm(trackForm);
        loadingDiv.style.visibility = "visible";
        trackForm.submit();
      }
      
    }
    Last edited by Ixonal; 09-05-2010 at 02:26 AM.

  2. #2
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: form submission differences?

    "loadingDiv.style.visibility = "visible";"

    I don't think that's js - try document.getElementById("loadingDiv").styl...

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  3. #3
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: form submission differences?

    A bigger question is why you seem to think that JavaScript validation would prevent you from using <input type="submit" />. JavaScript form validation (a mere user convenience) should be called from the form's onsubmit event, returning false on failure and true otherwise, and should be backed up by server-side (true) validation. Your page should work just as well with JavaScript disabled as it does with everything working as planned.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  4. #4
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: form submission differences?

    Quote Originally Posted by Alex Mac View Post
    "loadingDiv.style.visibility = "visible";"

    I don't think that's js - try document.getElementById("loadingDiv").styl...

    ~Callum
    it is JS, I have loadingDiv defined as a global for that element (multiple forms can load, so I just re-use that div)

    Quote Originally Posted by essellar View Post
    A bigger question is why you seem to think that JavaScript validation would prevent you from using <input type="submit" />. JavaScript form validation (a mere user convenience) should be called from the form's onsubmit event, returning false on failure and true otherwise, and should be backed up by server-side (true) validation. Your page should work just as well with JavaScript disabled as it does with everything working as planned.
    as for your comment, essellar, the site is based on AJAX, so it doesn't run at all without JS. I also want to do at least minor validation up front, as there are some things I don't even want getting to the server (there's validation there too).

    but my question still remains. they should be referencing the exact same browser function. so why is one sending post data and one not?
    Last edited by Ixonal; 09-05-2010 at 06:35 PM.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: form submission differences?

    Are you saying the form is submitted but there is no data sent?

    Does the page leave or is it an AJAX submission?
    Last edited by descalzo; 09-05-2010 at 07:07 PM.
    Nothing is always absolutely so.

  6. #6
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: form submission differences?

    Quote Originally Posted by descalzo View Post
    Are you saying the form is submitted but there is no data sent?

    Does the page leave or is it an AJAX submission?
    yeah, when I print out the $_POST array in php, it prints out an empty array if I use trackForm.submit(), but is populated properly if I use a regular submit input field.
    I have the target of the form as an iframe so it won't reload the whole page, but can still use the post method.

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: form submission differences?

    Let me guess.... lockForm() disables the text inputs, etc, right?
    Nothing is always absolutely so.

  8. #8
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: form submission differences?

    The onsubmit event is quite capable of doing "minor validation up front", and it is the ONLY correct way of calling validation on a form submission (that would not include a back-end POST request through XMLHttpRequest). You are aware that there are many more ways of submitting an HTML form than clicking your button, right? (If not, then you haven't done a lot of testing.) And no, calling the form's submit() method is not necessarily doing the same thing as using an <input type="submit">. For one thing, ALL buttons are submit buttons by default, so if you are not returning false to the button's onclick event on failure, the form will submit anyway and if your code passes, you get two competing calls to submit, one from your code and one from the button default event. Ooops!

    And if AJAX is the only way to use your site, you're doing it wrong.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  9. #9
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: form submission differences?

    and you have lost all credibility essellar, congrats.

    and yeah, lockform is there to disable all elements of the given form, so that you don't mess with it while it does its thing

  10. #10
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: form submission differences?

    Hint: disabled elements are not submitted.

    If you insist on disabling the input elements, you should try (don't know if it would work) disabling them immediately after submitting. Otherwise, use hidden elements.

    He is right on some things. A <button> will submit the form by default (at least in Firefox). Hitting [ENTER] will too. The validation/etc should come on the onsubmit of the form.
    Last edited by descalzo; 09-05-2010 at 07:54 PM.
    Nothing is always absolutely so.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. php form submission
    By Tenant in forum Programming Help
    Replies: 2
    Last Post: 02-07-2009, 10:16 AM
  2. REQ - PHP Form Submission Form
    By dwedno in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 11-29-2007, 06:38 PM
  3. Form Submission Apache Problems
    By EdisonTigersAlumni in forum Free Hosting
    Replies: 12
    Last Post: 11-28-2007, 12:52 PM
  4. Differences 2 Dir
    By ivbsav in forum Free Hosting
    Replies: 3
    Last Post: 08-25-2006, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers