-1

Why my code still loop submit form ?

When load page delay 1 sec and then submit form by javascript in the same time clearInterval so i want to know why my code still loop submit form ?

<?PHP
session_start();
include("connect.php");
?>

<form class="form" id="fid" method="post" action="" ENCTYPE = "multipart/form-data">
<input type="text" id="support" name="support" value="555">
</form>
<script>
    var timer_var = null;

    timer_var = window.setInterval(function (){
        document.getElementById("fid").submit();
        window.clearInterval(timer_var);
    }, 1000);
</script>

<?PHP
if(isset($_POST["support"]))
{
?>
<script>
alert("test");
</script>
<?PHP
}
?>
sjdm
  • 591
  • 4
  • 10

1 Answers1

1

Personally I would use setTimeout if it's a task that should be executed just once.

Let me explain you the problem. When the page loads you try to execute some code. Within this code you are submitting a form which will reload the page. This happens to cause the infinite loop.

If you want to submit data without reloading the page you could make an ajax request where you send the data that was entered in the input fields

Here you can find a very clear explanation: Submit form without reloading page

Y. Gherbi
  • 890
  • 8
  • 22