0

So i believe that automatic form submission is supposed to happen like this:

<form id="submit-me" ..... </form>

and then later....

<script type="text/javascript">
document.getElementById("submit-me").submit();
</script>

But for some reason, nothing happens when the page is loaded.

More specifically, the js script is only placed on the page if a particular PHP variable is equal to true. The next line of the PHP if statement immediately sets the variable back to false so that no looping occurs.

I am attaching a picture of the html that the PHP has rendered and I am circling the two parts that should be interacting but aren't. Theoretically, I should never be able to see this JS script and its form counterpart in the inspector because it would constantly be submitting, which means that the JS script isn't being triggered/ is not set up correctly. What do I need to do to trigger it and are there security issues that certain browsers like Chrome watch out for that prevent automatic form submission?

PICTURE OF HTML

Actual code:

echo '<form id="mainform" method="post" action="/bryan/modtourney.php?mode='.$mode.'&recursion='.$recursion.'">';   

I then add a big form and close it then a little later add this:

<?php
    if ($_SESSION['autoexecute'] == true)
{
$_SESSION['autoexecute'] =false;
?>
<script type="text/javascript">
    document.getElementById("mainform").submit(); 
</script>
<?php 
}
?>

There are console errors.

JS Console Error

  • Are there any errors in the Javascript console? – Barmar Sep 07 '17 at 01:51
  • Does your form have an input with `name="submit"`? See https://stackoverflow.com/questions/833032/submit-is-not-a-function-in-javascript – Barmar Sep 07 '17 at 01:53
  • yes there is a submit button – Daniel Taylor Sep 07 '17 at 02:01
  • Give it a different name, problem should be solved. – Barmar Sep 07 '17 at 02:03
  • I changed the name parameter to submitform instead of submit and retried but it didn't seem to have an effect. Note, I updated the main question with more info, there are console errors. Specifically it says there is no function called submit. I thought that the .submit() was a function inherent to the form... Also, just curious, what led you to believe that the name of the submit button was the issue? Thanks for your help BTW – Daniel Taylor Sep 07 '17 at 02:09
  • Oh, i just visited your other link, there are other buttons on the page that are also called submit so im assuming those are doing some over rides as well. Thank you so much! – Daniel Taylor Sep 07 '17 at 02:10
  • What led me to believe it? Because this is a common reason for `form.submit()` not to work. – Barmar Sep 07 '17 at 02:11
  • Those other buttons shouldn't affect this form. For historical reasons, all the inputs in a form become properties of the form object, and the property name is the name of the input. So if you have `name="submit"` in the form, then `document.getElementById("formid").submit` is that button element, and this overrides the `submit` function that's inherited from the `HTMLFormElement` type. – Barmar Sep 07 '17 at 02:14

0 Answers0