Decrement PHP variable by clicking a form button

Asked

Viewed 578 times

3

I want to decrement a numeric variable when I press a name button btn.

This is the code I tried:

$month = date("m");

$mes=1;
if(isset($_POST["btn"])) { 

    $month = $month-$mes;
    $mes++;

}

The problem is I always get the same value.

  • 2

    Yes, you are saying $mes is 1 and $Month is the current month, so you will subtract 1 from the current month whenever you click the button. You need to save the new value somewhere on the page, for example on the button itself or in some hidden field and use it in the next click.

2 answers

5


The problem is that you are zeroing out all PHP calls.

Here’s a very simplified example of how to persist data between one click and another:

<?php
   if( isset( $_POST['mes'] ) ) {
      $mes = 0 + $_POST['mes'];
   } else {
      $mes = 0 + date('m');
   }

   if( @$_POST['btn'] == '-' ) { 
      $mes--;
   } else if( @$_POST['btn'] == '+' ) {
      $mes++;
   }

   echo "Mes: $mes<br>";
   echo '<form method="post">';
   echo '<input type="submit" name="btn" value="-">';
   echo '<input type="submit" name="btn" value="+">';
   echo '<input type="hidden" name="mes" value="'.$mes.'">';
   echo '</form>';
?>


And before anyone complains, PHP deletion is for that. It’s to use where there is no problem. ISSET in such a case is that it makes no sense.

  • Thank you, but for example in the month=4, and I press + back to 12, and not to 5.

  • There was a syntax error in the parentheses, but it is exactly the same code that is here, see: http://ninja.net.br/akm.php

  • Be sure to copy the most up-to-date version, and test. Then you will gradually modify to your need, so when you change something that disturbs the functioning, you will realize what may have been the problem.

  • Yes works perfectly, Thanks

0

This function can also be done in javascript if necessary, and send only the month that was configured. I’ll give an example.

$("#btnEnviar").on("click" , function(){
  $.ajax({
    url : 'caminhodoarquivo.php' ,
    type : 'POST' ,
    dataType : 'json' ,
    data : { 
      mes : $("#txtMes").val()
    },
    sucess : function(resultado){
      //Faz o retorno para a o usuário
    },
    error : function( jqXHR, textStatus, errorThrown ){
      console.error(textStatus);
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

<form action="#" method="POST" id="frmMes" class="form">
  <input type="number" id="txtMes" min="1" max="12" />
  <button type="submit" id="btnEnviar" class="btb btn-primary">
    <span>Enviar</span>
  </button>
 </form>

Comments: Using the number (number input) element and the min and max properties, you avoid silly errors such as month 14 or -1.

I hope I’ve helped,

Douglas Dreer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.