开发者

click multiple times a submit button

开发者 https://www.devze.com 2023-02-04 19:45 出处:网络
I en开发者_运维百科countered a problem that I can\'t solve. My point is to \"click\" my sybmit button and every time increase a counter, while this counter reach 10. For the first time it works, but t

I en开发者_运维百科countered a problem that I can't solve. My point is to "click" my sybmit button and every time increase a counter, while this counter reach 10. For the first time it works, but this is it! My test code is below :

<form name="testForm" method="post">
<?php $cnt=0; ?>

<input type="submit" name="next" id="next" value="NEXT"/>

<?php
  if(isset($_POST['next'])){
    if($cnt< 10){
      echo $cnt.' --> ';
      $cnt++;
      echo $cnt;
    }
  }
?>
</form>

Only 0 --> 1 is printed, every time... please help!!

Thanks


Your counter isn't saved between page loads. What you need to do is save the counter to a $_SESSION variable


You should use sessions http://www.php.net/manual/en/book.session.php

<?php session_start()?>
<form name="testForm" method="post">
<?php 
    if (empty($_SESSION['cnt'])){
        $_SESSION['cnt'] = 0;
    }
?>

<input type="submit" name="next" id="next" value="NEXT"/>
<input type="submit" name="clear" id="clear" value="CLEAR"/>

<?php
    if(isset($_POST['next'])){
        if($_SESSION['cnt']< 10){
            echo $_SESSION['cnt'].' --> ';
            $_SESSION['cnt']++;
            echo $_SESSION['cnt'];
        }
    }
    if(isset($_POST['clear'])){
        $_SESSION['cnt'] = 0;
    }
?>
</form>


Your PHP code is being executed on the server, while the form is presented in the web browser. Every time the user clicks "submit" the form gets submitted to your server which will set the $cnt variable back to zero.

What you should do is put the count into the form, like:

<form name="testForm" method="post">

<?php $cnt = isset($_POST["cnt"]) ? $_POST["cnt"] : 0; ?>
<?php
  if(isset($_POST['next'])){
    if($cnt< 10){
      echo "<!-- ".$cnt.' --> ';
      $cnt++;
      echo $cnt;
    }
  }
?>
<input type="hidden" name="cnt" value="<?php echo $cnt;?>" />
<input type="submit" name="next" id="next" value="NEXT"/>
</form>

This way the current count will be part of the form. When the user hits "submit" they submit the current count and the fact that they submitted the form. Your server can then get the current count, increment it and send the form back with the new count.


You have to use session. first set session variable initial value 1 if it's not already initialized. After initialized, from 2nd click it will count +1 to every click, because it will satisfy the else statement

<?php
session_start();
?>
<html>
    <head></head>
    <body>
        <form method="post" action="submit.php">
            <input type="submit" name="count" value="Start counting" />
        </form>
        <?php
        if(isset($_POST['count'])){
            if(!($_SESSION['count'])){
                $_SESSION['count'] = 1;
            }else{
                $count = $_SESSION['count'] + 1;
                $_SESSION['count'] = $count;
            }
        }
        echo $_SESSION['count'];
        ?>
    </body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消