views:

67

answers:

3

I have a form that I need to submit automatically... (the fields are already filled and its complicated 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>
A: 
$("#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>
Gregoire
so I can just do: <input type="submit" onload="$("#mybuttonid").click();" /> or can I just do "click();"?
Luke3butler
...<input type="submit" id="mysubmit" /></form><script type="text/javascript"> $(document).ready(function(){$("#mysubmit").click();});</script>
Gregoire
awesome.. thanks for your help
Luke3butler
If this works, mark it as the accepted answer!
Sonny
ya i am trying it....
Luke3butler
Tried ALL the answers given... not working.. is there a way I can use a class instead of an ID?
Luke3butler
of course, just add a class to your submit button and call `$('.mysubmit').click()` instead; note the `.` instead of `#`
acmatos
yes.. thats what I thought it would be.... ( . instead of #) no success...
Luke3butler
Can you edit your question with your html source code please?
Gregoire
source is private.. but I will edit with applicable areas...
Luke3butler
Do you reference Jquery js files in your head section?
Gregoire
yes.. I have Jquery elements working fine on the site...
Luke3butler
for some reason I got something to work.. I guess I has more than 1 document ready function or something... idk.. but its working... this is my final result code....................................................................................... <?php if($_GET['print']==TRUE){ echo '<script type="text/javascript">$(document).ready(function(){$("#print").trigger("click");}); </script>'; } ?>
Luke3butler
you helped out the most.. hence the best answer...
Luke3butler
+2  A: 

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.

decyclone
so how would I do that?... <input type="submit" onload="submit();" /> ???
Luke3butler
A: 

Use .trigger()

$('#input_form_id').trigger('submit');

or trigger the click event handler from a submit button

$('#button_id').trigger('click');
jAndy