1

I'm trying to submit form without submit button. I used this code to submit: document.forms['form_id'].submit(); but this code submitin over and over again. I want submit just one time

<form method="POST" id="form_id">
<input type="hidden" id="lan" value="" name="number">
</form>


 <script type="text/javascript">

 document.forms['form_id'].submit();

 </script>
Joana Rudzis
  • 97
  • 1
  • 9
  • 1
    Is there a condition that must trigger for the form to be submitted? – td-edge Sep 24 '15 at 14:34
  • 3
    Because page is loaded and reloaded over and over again (after you POST page is reloaded and then submit() is called again and again and again and again...) – Adriano Repetti Sep 24 '15 at 14:36
  • 1
    if there are no conditions in this case i recommend using ajax. Please check [this post](http://stackoverflow.com/questions/19233254/jquery-submit-form-without-reloading-page) – Robert Sep 24 '15 at 14:37
  • Another solution if you only want it to submit once would be to set a cookie first, and always only submit if that cookie is not set. – miyalys Sep 25 '15 at 11:51

2 Answers2

0

the form is submitting it self everytime the javascript is loaded, which is everytime you load the page. The default behavior of a form is to submit to the location you are currently at, if not defined, you are continously submitting the form to the page that has the form and that again submits the form.

If you want this submit only to happen once, when a visitor is (re)directed to this page for instance, you should submit to a different one by using the action attribute of your form.

If you want the submit to happen on te request of a user, wrap your submit in a function that is called by an onclick event on a button, or any other event.

<script>
    function submittheform(){
        document.forms['form_id'].submit();
    }
</script>

<form method="POST" id="form_id" action="someHandlerUrl">
    <input type="hidden" id="lan" value="" name="number"/>
    <input type="button" onclick="submittheform()" value="submit the form"/>
</form>

you could use this script in the PHP building your page;

<?php
if(isset($_POST['number'])){
    $number = $_POST['number'];
    $out = '<h1>The form was submitted</h1>';
}else{
    $out = '
    <h1>The form needs to be submitted</h1>
    <form method="POST" id="form_id">
        <input type="hidden" id="lan" value="" name="number">
    </form>
    <script type="text/javascript">
        document.forms[\'form_id\'].submit();
    </script>
    ';
}

echo $out;

?>

When the form is submitted and the number value is present, it shows a page without the script and form.

DRGA
  • 169
  • 6
  • the thing is that i dont want to use any buttons. I want that code will submit one time and i dont need to redirect to another page because i need only transport the value from html tags to php code – Joana Rudzis Sep 25 '15 at 09:15
  • I've added another part to the solution – DRGA Sep 25 '15 at 11:39
  • where would you like your value to come from in the first place? In your demo form, the input has a value of "", which means nothing, the value is read out after the form is submitted, using the data in you $_POST array, what you do with it afterwards is up to you.. – DRGA Sep 25 '15 at 15:53
0
<form method="POST" action="" id="mailform">
<input type="email" name="mail" placeholder="EMAIL" id="mail">
<div id="sub" onclick="mailsubmit()">Click Me to Submit</div>
</form>
<script type="text/javascript">
function mailsubmit(){
document.getElementById("mailform").submit();
var mailid = document.getElementById("mail").value;
return mailid?alert(mailid):alert("You did not submit your Email");
}
</script> 
Lakshmi
  • 412
  • 3
  • 6