php increment variable value with 1 when submit

前端 未结 4 883
温柔的废话
温柔的废话 2020-12-17 04:20

Hi I am new in php and started learning. I am trying to increment variable value with 1 when submit button is pressed.

My Code:

<         


        
4条回答
  •  情歌与酒
    2020-12-17 04:32

    You're variable $i should be stored in session for example, so it won't lost its value when you submit the form. (You can check this answer Is PHP or PHP based web framework stateful or stateless?)

    Also when your script is executed, the first thing you do is $i = 0; so whenever you execute it, you reinitialize the variable to 0.

    session_start();
    
    // if your variable is not yet defined, you assigned it with 0
    if (isset($_SESSION['myVariable']))
    {
        $_SESSION['myVariable'] = 0;
    }
    
    if($_POST['submit'])
    {
        echo $_SESSION['myVariable'] +1;  
    }
    

提交回复
热议问题