最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Javascript autosubmit form - Stack Overflow

programmeradmin7浏览0评论

I have a form that I need to submit automatically... (the fields are already filled and its plicated to explain why but it IS necessary to do it this way).. I know how to autosubmit the form using Javascript but the only problem I have is that there is more than 1 submit button.. and I need 1 in particular to be submitted... thanks in advance

EDIT2(source):

<I put the javascript in the head... />
<FORM ACTION="PDF.php" name="form" METHOD="post">
<A whole bunch of inputs />
<INPUT TYPE="submit" name="form-save" VALUE="Save Changes" >
<INPUT TYPE="submit" name="form-submit" VALUE="Submit" >
<input type="submit" name="print" id="print" value="Download PDF" />
</form>

I have a form that I need to submit automatically... (the fields are already filled and its plicated to explain why but it IS necessary to do it this way).. I know how to autosubmit the form using Javascript but the only problem I have is that there is more than 1 submit button.. and I need 1 in particular to be submitted... thanks in advance

EDIT2(source):

<I put the javascript in the head... />
<FORM ACTION="PDF.php" name="form" METHOD="post">
<A whole bunch of inputs />
<INPUT TYPE="submit" name="form-save" VALUE="Save Changes" >
<INPUT TYPE="submit" name="form-submit" VALUE="Submit" >
<input type="submit" name="print" id="print" value="Download PDF" />
</form>
Share Improve this question edited Jun 23, 2010 at 18:15 Sean Vieira 160k34 gold badges320 silver badges296 bronze badges asked Jun 23, 2010 at 15:07 Luke3butlerLuke3butler 2102 silver badges13 bronze badges 1
  • 1 You really should explain why you think you need to auto-submit a form, because usually it's a sign of using the wrong technology to solve the wrong problem. Hint: What about users without JS? – RoToRa Commented Jun 23, 2010 at 15:10
Add a ment  | 

4 Answers 4

Reset to default 4

instead of going for a click event on a submit button, you can call submit of a form object from javascript.

Example :

<head>
<title>Auto Submit Form</title>
<script type="text/javascript">
    window.onload = function () {
        var form = document.getElementById("PDFGenerationForm");
        form.submit();
    };

    function OnFormSubmit() {
        alert("Submitting form.");
    }
</script>

<body>
<form id="PDFGenerationForm" action="" method="post" onsubmit="OnFormSubmit">
    <!--Any input tags go in here-->
</form>

This editor won't let me paste the whole HTML in here. So, it is in fragments.

$("#yourbuttonid").click();

EDIT:

<form>
    ... 
   <input type="submit" id="myFirstsubmit" /> 
   <input type="submit" id="mysubmit" /> 
</form> 
<script type="text/javascript">  
    $(document).ready(function(){$("#mysubmit").click();}); 
</script>

If you really want to click a specific button, add this script to the end of your page:

<script type="text/javascript">
    // press the button
    var myButton = document.getElementById("idOfTheButtonToClick");
    myButton.click();
</script>

This assumes your button has an ID.

1) Here is a working auto-submit method: when page is loaded, the form will be immediately autosubmited (the values can be set with php variables too.)

<form action="page.php" name="FORM_NAME" method="post">
<input type="text" name="example1" value="YOUR_VALUE" />
<input type="submit" />
</form>
<SCRIPT TYPE="text/JavaScript">document.forms["FORM_NAME"].submit();</SCRIPT>

or use for any form on that page:

document.forms[0].submit();

2) you can use button-click (called after 1 second):

<SCRIPT TYPE="text/JavaScript">setInterval(function () {document.getElementById("myButtonId").click();}, 1000);</SCRIPT>
发布评论

评论列表(0)

  1. 暂无评论