SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automatic form submission error

    Hi,

    I am writing a very simple script to automatically submit a form when the page loads. I am getting the "object doesn't support this property or method" error. I must be missing something obvious! Anyone any suggestions?

    Here's the code:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    <body>
    
    <form name="myForm" action=" " method="post">
    ...	
    </form>
    
    <script type="text/javascript">
    document.myForm.submit();
    </script>
    
    </body>
    </html>
    Thanks,
    cph.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,169
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Try something like
    HTML Code:
    var theform = document.getElementById('myForm');
    theform.submit();
    But take note, as the code is posted, it will endlessly submit the form upon loading and reloading.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since the OP has not defined the ID of form tag, I think it is better to do it like this:
    Code javascript:
    var theform = document.myForm;
    theform.submit();
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for those suggestions Mittineague and rajug, but the object still doesn't want to suport the property/method!

    The form works fine when submited with a button, it's just the document.myForm.submit(); line that is producing an error. It doesn't seem to matter what way I write the code (e.g. I have also tried calling a function using the onload event handler).

    It's such a simple piece of code that I can't imagine what I'm doing wrong, but it must be something obvious! Anymore suggestions would be welcomed...

    cph

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What have you put in the action attribute of your form? Give proper your or leave blank if you want to post in the same page.
    HTML Code:
    <form name="myForm" action="" method="post">
    ...............
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The action attribute contains the url of the cgi file that handles the form, for the purposes of posting on this forum I left the action attribute blank. The form works perfectly well (it submits with a button), it is the JavaScript code that is producing an error.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...having said it was the JavaScript...I just tried the code on a different form and it worked fine. So it must be something to do with the form that is causing the JavaScript error.

    The form is a PayPal button:

    HTML Code:
    <form name="myForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    	<input type="submit" name="submit" value="Continue">
    	<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
    	<input type="hidden" name="cmd" value="_xclick-subscriptions">
    	<input type="hidden" name="item_name" value="Service">
    	<input type="hidden" name="page_style" value="Primary">
    	<input type="hidden" name="no_shipping" value="1">
    	<input type="hidden" name="no_note" value="1">
    	<input type="hidden" name="currency_code" value="GBP">
    	<input type="hidden" name="lc" value="GB">
    	<input type="hidden" name="bn" value="PP-SubscriptionsBF">
    	<input type="hidden" name="a3" value="10.00">
    	<input type="hidden" name="p3" value="1">
    	<input type="hidden" name="t3" value="M">
    	<input type="hidden" name="src" value="1">
    	<input type="hidden" name="sra" value="1">
    	</form>
    Does anyone know why this won't allow me to submit automatically? (In case you're wondering why I want to submit this automatically it is to reduce the number of steps in a sign-up process after the user has already filled in a sign-up form).

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works fine in my case:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Auto submit form</title>
    </head>
    <body>
    <form name="myForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    	<input type="submit" name="submit" value="Continue">
    	<img alt="" border="0" src="https://www.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
    	<input type="hidden" name="cmd" value="_xclick-subscriptions">
    	<input type="hidden" name="item_name" value="Service">
    	<input type="hidden" name="page_style" value="Primary">
    	<input type="hidden" name="no_shipping" value="1">
    	<input type="hidden" name="no_note" value="1">
    	<input type="hidden" name="currency_code" value="GBP">
    	<input type="hidden" name="lc" value="GB">
    	<input type="hidden" name="bn" value="PP-SubscriptionsBF">
    	<input type="hidden" name="a3" value="10.00">
    	<input type="hidden" name="p3" value="1">
    	<input type="hidden" name="t3" value="M">
    	<input type="hidden" name="src" value="1">
    	<input type="hidden" name="sra" value="1">
    	</form>
    <script>
    document.myForm.submit();
    </script>
    </body>
    </html>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Copied that code, but still doesn't autosubmit when I try (tried in IE7 and FF).

    Thanks anyway.

  10. #10
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oddly, the code that rajug posted only works when I remove the following line:

    Code html4strict:
    <input type="submit" name="submit" value="Continue">
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found it works when I remove that line too. AND interestingly, it also works with:

    <input type="image" src="file.gif" border="0" name="submit">

    I don't see why using an image instead of a button makes such a difference, but I expect someone out there knows!

    Thanks for everyones input on this


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
  •