Automatically click a html button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buddy000
    New Member
    • Mar 2008
    • 4

    Automatically click a html button

    hi,

    I am new to web development. I developed a simple html page with submit button which calls a php script once clicked. Following snippet show the scenario.
    [code=html]
    <html>
    <body>
    <br>
    <form method="post" action="handles cen.php">
    <p style="font-family:helvetic a;font-size:14px">
    <BR><BR>
    <INPUT TYPE=SUBMIT NAME="SUBMIT4" VALUE="Get Statistics">
    </P>
    </FORM>
    </BODY>
    </HTML>
    [/code]
    I need to add the functionality that "SUBMIT4" button is automatically clicked after certain time (10 secs e. g) repeatedly. Any idea to do this will be greatly appreciated.

    Amir
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to the Scripts.

    Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

    This is a JavaScript / DHTML question, so I will move this thread over to the JavaScript forum.

    moderator

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by buddy000
      hi,

      I am new to web development. I developed a simple html page with submit button which calls a php script once clicked. Following snippet show the scenario.
      [code=html]
      <html>
      <body>
      <br>
      <form method="post" action="handles cen.php">
      <p style="font-family:helvetic a;font-size:14px">
      <BR><BR>
      <INPUT TYPE=SUBMIT NAME="SUBMIT4" VALUE="Get Statistics">
      </P>
      </FORM>
      </BODY>
      </HTML>
      [/code]
      I need to add the functionality that "SUBMIT4" button is automatically clicked after certain time (10 secs e. g) repeatedly. Any idea to do this will be greatly appreciated.

      Amir
      The thing which you want to achieve can be achieved by other ways too, but if you want do that by auto-submitting, then add function autoSubmit(10) to the onload attribute of body tag.
      And following to the <script> tag.[code=javascript]var t = 0;
      function autoSubmit(sec) {
      if (t<sec) var wait = window.setTimeo ut("autoSubmit( "+sec+")", 1000);
      else document.forms[0].submit();
      t++;
      }[/code]
      Give it a try, tell me if it works...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        another possibility would be to use the setInterval() method:

        [CODE=javascript]
        function submit_form() {
        document.forms[0].submit();
        }

        function autoSubmit() {
        window.setInter val(submit_form , 10000);
        }[/CODE]
        kind regards

        Comment

        • buddy000
          New Member
          • Mar 2008
          • 4

          #5
          I have multiple buttons in the html form. Is is also possible to "set" a specific button? In php script, i am checking that through if(isset($_POST['SUBMIT4'])) condition.

          Best regards,
          Amir

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            could you explain that requirement in more detail? may be with some example code?

            kind regards

            Comment

            • buddy000
              New Member
              • Mar 2008
              • 4

              #7
              Originally posted by gits
              could you explain that requirement in more detail? may be with some example code?

              kind regards
              Hi,

              Here is structure of HTML page with single form and multiple button
              Code:
              <html> 
                      <body> 
               
                      <form method="post" action="handlescen.php"> 
              <p style="font-family:helvetica;font-size:14px"> 
                      Select the scenario to start 
                      <select name="scenario"> 
              </select> 
              <BR><BR> 
              <INPUT TYPE=SUBMIT NAME="SUBMIT1" VALUE="Start!"> 
              <INPUT TYPE=SUBMIT NAME="SUBMIT2" VALUE="Stop!"> 
              <INPUT TYPE=SUBMIT NAME="SUBMIT3" VALUE="Back"> 
              <INPUT TYPE=SUBMIT NAME="SUBMIT4" VALUE="Get Statistics"> 
              </P> 
              </FORM> 
              </BODY> 
              </HTML>
              I am handling the different button clicks in following Handlescen.php file.

              Code:
              <?php
              ob_start();
              ?>
              <html><body>
              <?php
              
              if(isset($_POST['SUBMIT1'])){
              
              }
              
              if(isset($_POST['SUBMIT2'])){
              
              }
              
              if(isset($_POST['SUBMIT3'])){
              
              }
              if(isset($_POST['SUBMIT4'])){
                // show some results
              }
              ?>
              </body></html>
              Once SUBMIT4 button is clicked, I want to enable automatic clicking of button4 after evering 10 seconds. It may look bit weird requirement but I still want to give it a try.

              Regards,
              Amir

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Try this...
                [php]<html>
                <head>
                <?php
                if (isset($_GET['auto_submit']))
                echo "<script>
                function autoSubmit() {
                -------- //CODE GIVEN ABOVE//MAKE SURE YOU ADD SLASHES!
                }
                <script>";
                ?>
                </head>
                <body<?php
                if (isset($_GET['auto_submit']))
                echo " onload=\"autoSu bmit()\"";
                ?>>
                <form method="post" action="handles cen.php">
                <p style="font-family:helvetic a;font-size:14px">
                Select the scenario to start
                <select name="scenario" ></select>
                <br><br>
                <input type=submit name="submit1" value="start!">
                <input type=submit name="submit2" value="stop!">
                <input type=submit name="submit3" value="back">
                <input type=submit name="submit4" onclick="addget ();" value="get statistics">
                </p>
                </form>
                </body>
                </html>[/PHP]
                handlescen.php
                Redirect from here to the required php file.
                [PHP]<?php
                ob_start();
                if(isset($_POST['SUBMIT1'])){
                header("locatio n:abc1.php");
                }

                if(isset($_POST['SUBMIT2'])){
                header("locatio n:abc2.php");
                }

                if(isset($_POST['SUBMIT3'])){
                header("locatio n:abc3.php");
                }
                if(isset($_POST['SUBMIT4'])){
                header("locatio n:handlescen.ph p?auto_login");
                ?>[/PHP]

                BTW: both snippets are from same file?

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  And for the submit problem, dynamically change the action value of the form when you click on a submit button. Concat a get parameter to the action, different for all the submit buttons.

                  Comment

                  • buddy000
                    New Member
                    • Mar 2008
                    • 4

                    #10
                    Thanks hsriat,

                    I tried your proposed code but it does not seem to work. Instead it prints following text on webpage
                    [HTML]function submit_form() { document.forms[0].submit();}func tion autoSubmit() {window.setInte rval(submit_for m, 10000);}"; ?> >[/HTML]

                    To rule out the multiple line string problem, i wrote the complete text in single line.

                    Code:
                    <html>
                            <head>
                            <?php
                            if (isset($_GET['auto_submit']))
                                echo "<script = \"text/javascript\"> function submit_form() { document.forms[0].submit();}function autoSubmit() {window.setInterval(submit_form, 10000);}</script>";
                            ?>
                            </head>
                            <body<?php
                                    if (isset($_GET['auto_submit']))
                                      echo " onload=\"autoSubmit()\"";
                                    ?>
                            >
                            <br>
                            <form method="post" action="handlescen.php">
                    
                    <INPUT TYPE=SUBMIT NAME="SUBMIT1" VALUE="Start!">
                    <INPUT TYPE=SUBMIT NAME="SUBMIT2" VALUE="Stop!">
                    <INPUT TYPE=SUBMIT NAME="SUBMIT3" VALUE="Back">
                    <INPUT TYPE=SUBMIT NAME="SUBMIT4" onclick="addget();" VALUE="Get Statistics">
                    
                    </FORM>
                    </BODY>
                    </HTML>
                    Please let me know if you see some problem in here

                    Best regards,
                    Amir

                    Comment

                    Working...