SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery - automatically submitting a form

    I'm sure this is really easy, but I can't get it to work

    I'm trying to submit a form full of hidden fields automatically when the page loads. I don't have access to the <body> tag, so I'm trying to add the auto submit with javascript. (jqeury)

    I've got so far:
    Code HTML4Strict:
    <form id="paypalpayment" name="paypalpayment" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    //bunch of hidden form fields
    <input name="submit" type="submit" id="submit" value="Click to continue if you are not automatically redirected." />
    </form>
     
    <script type="text/javascript"> 
    jQuery(document).ready(function($) {
    $("paypalpayment").submit();
    }); 
    </script>

    But nothing happens...

    Any suggestions?

    btw this is within a wordpress page.

    i've tried the regular "$(document).ready(function() {" as well.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    It will be much easier for us to debug your problem if we can examine the page that's having trouble.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately this is on my local server

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    What does your browser's error console / javascript console (what browser are you testing with) have to say regarding this?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firefox

    Showing no error.

    I added a debug paragraph <p> and changed the javascript to:
    $(document).ready(function() {
    $("paypalpayment").submit();
    $("p").text("We are here");
    });


    The last line of the javascript modifies the added debug paragraph without any troubles, suggesting the .submit() line isn't causing an error, just does nothing.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,460
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    You can't alway just drop code into a WordPress theme file and have it work. Did you use wp_enqueue_script("jquery"); ??

    and if you are using other javascript libraries (i.e. prototype), you should use jquery in "no conflict" mode. i.e. $j instead of $

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what wp_enqueue_script is (I need to check that)

    I think it is in "no conflict" mode, was just a free theme Im using

  8. #8
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've uploaded it live:
    http://tinyurl.com/yhvulfu

  9. #9
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without a <body> tag document object references won't work. Try...

    $(window).ready(function(){
    // do stuff here
    });

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $("paypalpayment") looks for html elements like <paypalpayment>

    You probably meant to find an element by id.

    #paypalpayment

  11. #11
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for your responses. U are correct crmalibu, thats what I meant.

    Ive tried changing live site to $(window) and corrected to #paypalpayment and still isnt working for me.

  12. #12
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $(window).load(function () {
    // run code
    });

    May actually be more suited to your needs...

  13. #13
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestion, changing to "load" didn't solve the problem.

  14. #14
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    load and ready event handlers tend to conflict allot... I wold suggest adding the load event to the element...

    <form id="paypalpayment" name="paypalpayment" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    //bunch of hidden form fields
    <input name="submit" type="submit" id="submit" value="Click to continue if you are not automatically redirected." />
    </form>

    <script type="text/javascript">
    $("#paypalpayment").load(function($) {
    $("#paypalpayment").submit();
    });
    </script>

  15. #15
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Getting some weird error:
    Error: syntax error
    Source File: ..../paypal-payment-submit/
    Line: 105, Column: 29
    Source Code:
    $("#paypalpayment").submit();</p>

  16. #16
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are editing in the design view... hence the <p></p> inserts in between your script tags... You might want to switch to code view and remove those tags...

  17. #17
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, I wasn editing in HTML view not Visual view.
    Anyway i've removed all line spaces from javascript code.
    No error now, but still doesn't work.

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    How would you suggest that we duplicate in some way your situation, so that instead stabbing blindly in the dark, someone here can actually help you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #19
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pmw57

    I did upload it and put a link to it earlier, see http://tinyurl.com/yhvulfu

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    I'm seeing conflicts with the submit button that's also called submit.

    Try this instead to see if it works as a work-around.

    $('#submit').click();
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #21
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, not working for me.

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    No not like this:
    Code javascript:
    jQuery("#paypalpayment").load(function($) {$('#submit').click();});

    Like this:
    Code javascript:
    $('#submit').click();
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  23. #23
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank YOU! Works a charm!

  24. #24
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    You're welcome.

    You really should place the script away from the central content of the page. Either in the head of the page, or at the end of the body just before the </body> tag.

    In either case, you should wrap the jQuery statement with an onready wrapper.

    Code javascript:
    $(function () {
        $('#submit').click();
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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