0

I'm have a simple form, when clicking the submit button the backend php file gets executed by my JavaScript file, results are returned via ajax, everything works great when manually clicking the button. However, when trying to use javascript to automatically submit the form every 120 seconds it is not working. The javascript never get's called which in turn causes the php to not execute...

html

<form id="send-with-ajax" name="ping">
    <input type="submit" value="Execute Ping" />
    <div class="ajax-response"></div>
</form>

<script type="text/javascript">
 // refresh ping results every 120 seconds
 var pingRefreshInterval = setInterval(function () {
   console.log(
     'submitting ping.php request after 120 second wait - time now:'+new Date());
      document.getElementById("send-with-ajax").submit();
      },120000); 
</script>

<script src="js/portal.js"></script>

Again, there are not issues with my portal.js file or my php file -- The main thing to note here is that the document.getElementById("send-with-ajax").submit(); does not do anything... Any ideas?

6
  • Have you tried jQuery way? $('#send-with-ajax').submit() Commented Feb 5, 2014 at 17:03
  • 2
    Does the console throw any errors? The form could probably do with a method and action as well. Commented Feb 5, 2014 at 17:04
  • Tested your code outside of your environment and it works fine. jsfiddle.net/v5Z85. So, must be something missing outside of that function. Commented Feb 5, 2014 at 17:07
  • 1
    You could also try $('input[type=submit]').click(); instead of re-submitting the form. But is submitting the form even necessary in the first place? My guess is that js/portal.js is just executing a function when it senses a post event, so couldn't you just call the function in your interval? Commented Feb 5, 2014 at 17:08
  • @beercodebeer Thanks for the info, I was keeping the form around because I actually needed to do things both ways (automatically and with a submit button). I've changed a few things and am now just triggering my JS function, thanks for sparking some thought in my brain. Nice name BTW. If you post your commend and an answer I'll be sure to check mark it for you. Thanks again. Commented Feb 6, 2014 at 3:46

3 Answers 3

1

Assuming that js\portal.js just executes a function when a POST event occurs, you could simply call that function from inside your interval.

If that won't work for some reason (maybe there's a lot of other stuff happening on your page that you didn't show us), and your call to submit() isn't working properly, you could also trigger a button click with $('input[type=submit]').click(); (assuming you only have one submit button on the page - otherwise add a class or an id and trigger on that instead).

Sign up to request clarification or add additional context in comments.

Comments

1

Try using the submit() method. Or use AJAX.

Comments

1

If you are doing things on onsubmit event, document.getElementById("send-with-ajax").submit() doesn't trigger this event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.