form submit with javascript

Hi there,
hopefully someone can point me in the right direction here. I have a webpage with a form. I need to submit the form automatically and I am using Javascript.

Now when I actually push the submit button, the form is posted to its destination and does what its supposed to. However when I use the Javascript call it, it just reloads the page with the form repeatly. It appears to be repeatidly submitting it and not processing the data in the form.

I have put some code below.

All suugestions welcome

Thanks

The form

<form id="form1" method="post">    
    <table width="400px">
             <tr id="drpTr2">
            <td class="text_desc">
                <label class="desc_text">Success point/percent :</label>
            </td>
            <td>
                 <!--<input style="width:50px" type="text" name="txtSuccessP" id="txtSuccessP" value="<?php // echo xyz96zyx::xyz100zyx("txtSuccessP") ?>"  /> -->
                 
                 <input style="width:50px" type="text" name="txtSuccessP" id="txtSuccessP" value="90"  />
            </td>
        </tr>
               <tr id="drpTr3">
            <td class="text_desc">
                <label class="desc_text">Test time (in minutes) :</label>
            </td>
            <td>
          <!--       <input style="width:50px"  type="text" name="txtTestTime" id="txtTestTime" value="<?php// echo xyz96zyx::xyz100zyx("txtTestTime") ?>"  /> -->
                 <input style="width:50px"  type="text" name="txtTestTime" id="txtTestTime" value="45"  />
         
            </td>
        </tr>
    </table>

    <br>
    <hr />
    <table width="500">
        <tr>
            <td colspan="2">
                <input id="btnLcl" type="button" onclick="ShowUsers('local')" value="Local users" style="border:0;width:150px;color:red" />&nbsp;<input id="btnImp" type="button" onclick="ShowUsers('imported')" value="Imported users" style="border:0;width:150px" />
            </td>            
        </tr>
        <tr>
            <td valign="top" id="tdLocalUsers">
                 <!--<div id="div_grid"><?php //echo $grid_html ?></div> -->
                 <input  type=checkbox name=chkgrd[] value= <?php echo $_SESSION['user_id'] ?> checked />
            </td>
           
            <td valign="top" id="tdImportedUsers" style="display:none">
                    <!--<div id="div_grid"><?php // echo $imported_grid_html ?></div>-->
            </td>
           
        </tr>
    </table>
   
    <table>
        <tr>
            <td><input onclick="return CheckForm()" style="width:100px" type="submit" id="btnSave" name="btnSave" value="Save" /></td>
            <td><input onclick="javascript:window.location.href='index.php?module=assignments'" style="width:100px" type="button" id="btnCancel" name="btnCancel" value="Cancel" /></td>
        </tr>
    </table>

</form>

// This is the javascript

<?php

      echo   '
           <script type="text/javascript">
                  
                  document.forms["form1"].submit();
      </script>' ;
?>
williery10Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Where is the checkForm() function?
Rainer JeschorCommented:
Hi,
as the form gets submitted automatically due to the used JavaScript code and as you have not set any action URL, the post will go to the same page, renders the form, does a JavaScript form submit to the same URL, gets the same page, renders the form ...

You should set a hidden input field before doing the JavaScript submit:
Form
<form id="form1" method="post">    
     <table width="400px">
              <tr id="drpTr2">
             <td class="text_desc">
                 <label class="desc_text">Success point/percent :</label>
             </td>
             <td>
                  <!--<input style="width:50px" type="text" name="txtSuccessP" id="txtSuccessP" value="<?php // echo xyz96zyx::xyz100zyx("txtSuccessP") ?>"  /> -->
                  
                  <input style="width:50px" type="text" name="txtSuccessP" id="txtSuccessP" value="90"  />
             </td>
         </tr>
                <tr id="drpTr3">
             <td class="text_desc">
                 <label class="desc_text">Test time (in minutes) :</label>
             </td>
             <td>
           <!--       <input style="width:50px"  type="text" name="txtTestTime" id="txtTestTime" value="<?php// echo xyz96zyx::xyz100zyx("txtTestTime") ?>"  /> -->
                  <input style="width:50px"  type="text" name="txtTestTime" id="txtTestTime" value="45"  />
           
             </td>
         </tr>
     </table>

     <br>
     <hr />
     <table width="500">
         <tr>
             <td colspan="2">
                 <input id="btnLcl" type="button" onclick="ShowUsers('local')" value="Local users" style="border:0;width:150px;color:red" />&nbsp;<input id="btnImp" type="button" onclick="ShowUsers('imported')" value="Imported users" style="border:0;width:150px" />
             </td>            
         </tr>
         <tr>
             <td valign="top" id="tdLocalUsers">
                  <!--<div id="div_grid"><?php //echo $grid_html ?></div> -->
                  <input  type=checkbox name=chkgrd[] value= <?php echo $_SESSION['user_id'] ?> checked />
             </td>
             
             <td valign="top" id="tdImportedUsers" style="display:none">
                     <!--<div id="div_grid"><?php // echo $imported_grid_html ?></div>-->
             </td>
             
         </tr>
     </table>
     
     <table>
         <tr>
             <td><input onclick="return CheckForm()" style="width:100px" type="submit" id="btnSave" name="btnSave" value="Save" /></td>
             <td><input onclick="javascript:window.location.href='index.php?module=assignments'" style="width:100px" type="button" id="btnCancel" name="btnCancel" value="Cancel" /></td>
         </tr>
     </table>
     <input type="hidden" id="FormAlreadySubmitted" value="0" />
 </form>

Open in new window

Javascript:
<?php

       echo   '
            <script type="text/javascript"> 
                   if (document.getElementById('FormAlreadySubmitted').value == 0) {
                     document.getElementById('FormAlreadySubmitted').value = 1;
                     document.forms["form1"].submit();
                  } 
       </script>' ;
 ?> 

Open in new window

and the PHP side has to set the value as posted.

Just my 2 ct
Rainer
Slick812Commented:
greetings     williery10, , your javascript really does not do anything useful  as -

<script type="text/javascript">
  document.forms["form1"].submit();
</script>' ;

But the much more Useful way to to have javascript run on a <form> submit is to use the form onsubmit event assignment -

<form name="form1" onsubmit="return validate(this)" method="post" action="ppd.php">

you should NOT use the submit button events (onclick) assignments to do form validations or other javascript for the FORM it's in, as it can be misdirected in some methods.

you might look at this tutorial -
http://www.javascriptkit.com/javatutors/form6.shtml

ask questions if you need other info

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.