44

I'm having a bit of trouble with my current issue. Any help would be greatly appreciated.

I have a step in a 'signup process' which I don't need anymore, but I don't have time to reconfigure the entire process so I'm trying to auto submit the form on page load so it will basically skip over this step. Any thoughts?

EDIT: Sorry, I should mention, originally there were two submit options, I got rid of one, now I just want to submit the 'mem_type' option on page load. not sure if that makes much of a difference.

<form method=post action="<?=$base_href.url("signup")?>" name="member_signup">
<input type=hidden name="process" value="facility_info">
<input type=hidden name="create_order" value="true">

<?php
    foreach ($_POST as $k=>$d) {
        if ($k === 'textarea') continue;
        echo "<input type=hidden name=\"".strip_tags($k)."\" value=\"".strip_tags($d)."\">";
    }
?>

<input type="submit" value="submit" name="mem_type" border="0">
</form>
Lain
  • 2,166
  • 4
  • 23
  • 47
MDez
  • 671
  • 1
  • 5
  • 11

5 Answers5

83

Try this On window load submit your form.

window.onload = function(){
  document.forms['member_signup'].submit();
}
Pupil
  • 23,834
  • 6
  • 44
  • 66
Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 2
    thsi worsk perfect but somehow it tries to open the form on a popup so I gets blocked by browser, any idea how can I fix this? – Spring Sep 14 '15 at 10:29
  • @Spring In situations like this, make sure you don't have a "target" on your form that points away from the page. (eg. `target="_blank"`) – Chuck Le Butt Jan 25 '21 at 12:39
24

You can submit any form automatically on page load simply by adding a snippet of javascript code to your body tag referencing the form name like this....

<body onload="document.form1.submit()">
showdev
  • 28,454
  • 37
  • 55
  • 73
Bob Wood
  • 249
  • 2
  • 3
  • Exactly what I was looking for. Thank you! – Erik Kalkoken Jan 25 '17 at 19:03
  • However, with IE11, I'm loosing my URL parameters when performing so. – hublo Feb 26 '18 at 16:23
  • 4
    I was stuck for a few hours today, unable to get this to work, even though I have implemented something like this many times. The problem turned out to be my form contained a input with type="submit" name="Submit" id="submit". Removing the id attribute fixed it, I don't understand exactly why this is, especially with the cross up of id and name and but I am posting it here in case someone is having a problem submitting a form this way. I'd say watch out for names in the DOM colliding with the name of the function you need to run. – DanAllen Dec 27 '18 at 00:19
11

Add the following to Body tag,

<body onload="document.forms['member_signup'].submit()">

and give name attribute to your Form.

<form method="POST" action="" name="member_signup">
Qrazier
  • 162
  • 4
  • 15
Jijesh Cherayi
  • 1,111
  • 12
  • 15
3

If your form doesn't have an id you can use,

<body onload="document.forms[0].submit()">

This will submit the first form found on the page.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
1

This is the way it worked for me, because with other methods the form was sent empty:

<form name="yourform" id="yourform" method="POST" action="yourpage.html">
    <input type=hidden name="data" value="yourdata">
    <input type="submit" id="send" name="send" value="Send">
</form>
<script>            
    document.addEventListener("DOMContentLoaded", function(event) {
            document.createElement('form').submit.call(document.getElementById('yourform'));
            });         
</script>
Leopoldo Sanczyk
  • 1,529
  • 1
  • 26
  • 28