Change value of a variable with PHP button

Asked

Viewed 2,913 times

-2

I want to change the value of this variable.

$beg_j = $numjour; 

For this,

$beg_j = $numjour-1; 

button:

echo "<input type=button value=Change onclick=$beg_j-1>";
  • 5

    Looks like you’re looking for AJAX. But I think you need to explain what problem you’re trying to solve not just the code that doesn’t work or that you want it to work.

2 answers

1

This is not possible. php runs on the server and after running, everything is on account of the client (browser).

When the browser requests the server, php is executed and returns the result (html) to the browser. This is its lifecycle. It does not remain running as is the case with javascript.

If you want to run a php script when the user clicks a button, without reloading the page, then you should study about AJAX.

-1

The right thing would be for you to put in something more complete than you already have and are trying to do, but I’ll try to give you an idea so you can try to implement, so you won’t need to reload the page to decrease or change the value of your variable, anything post here your difficulties.

Use ajax to send the value to a php page where there will be a variable that will receive this value and change its value according to your need, example: in your javascript contained in your main page do:

var url = "seuArquivo.php";
var data = "numjour="+valor;//Na variável "valor" vc pode atribuir um valor específico vindo do seu form por exemplo.
$.ajax({                 
        type: 'POST',                 
        //dataType: 'json',                 
        url: url,                 
        async: true,                 
        data: data,                 
        success: function(response) {
            $("#idSuaDiv").val(response);
            //Isso colocaria o resultado em sua div, mas vc pode fazer outras coisas aqui com a resposta
        }             
    });

In your file . php would just need to take the value and do something with it or process it the way you want, example:

 $beg_j = $_POST["numjour"];
    //agora faça o que vc quiser com a variável e depois mande a resposta de volta pro form.
print_r($beg_j);

Browser other questions tagged

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