0
I have the code below, but it is not obeying the comparison of if
's, always puts the last value even without going through it, follows code:
<?php $minhavar = 100; ?>
<!DOCTYPE html>
<html>
<body>
<h1>Teste</h1>
<p>Trocar valor da variavel php</p>
<form name="form1">
<input type ="text" id="varhtml" value = 1 >
<button onclick="myFunction()">Click me</button>
</form>
<p id="demo"></p>
<script>
function myFunction() {
var item = form1.varhtml.value;
if (item == 1){
alert("<?php
$minhavar = 1200;
echo $minhavar; ?>") ;
}
if (item == 2){
alert("<?php
$minhavar = 2200;
echo $minhavar; ?>") ;
}
if (item == 3){
alert("<?php
$minhavar = 3200;
echo $minhavar; ?>") ;
}
//sempre esta mostrando o ultimo valor mesmo o item nao sendo 3
alert("<?php echo $minhavar; ?>") ;
document.getElementById("demo").innerHTML = "entrou na funcao javascript";
}
</script>
</body>
</html>
The problem continues (I need to change the value of the PHP variable $minhavar
) see code below:
<?php
$minhavar = 100;
?>
<!DOCTYPE html>
<html>
<body>
<h1>teste</h1>
<p>trocar valor da variavel php</p>
<form name="form1">
<input type ="number" id="varhtml" name = "varhtml" value = 1 >
<button onclick="myFunction()">Click me</button>
<button onclick="vervalor()">Ver Valor variavel </button>
</form>
<p id="demo"></p>
<script>
function myFunction() {
var item = form1.varhtml.value;
if (item == 1) {
alert("<?php $minhavar = 1200; echo $minhavar; ?>");
}
else if (item == 2){
alert("<?php $minhavar = 2200; echo $minhavar; ?>") ;
}
else if (item == 3){
alert("<?php $minhavar = 3200; echo $minhavar; ?>") ;
}
}
function vervalor(){
alert("<?php echo $minhavar; ?>") ;
}
</script>
</body>
</html>
Good Morning Gustavo, you still have the same problem. what I need is to change the value of a PHP variable $minhavar depending on the chosen value. When I went to see the page’s view-souce it already runs all <?php and always prevails the last one. I’ll leave the new code below to see with a button to show the current value of the variable PHP $minhavar.
– mariofel
I saw that the Ifs it even performs correctly the problem is in the <?php statements that are executed soon when I call the page.
– mariofel
@mariofel I edited the answer trying to clarify the doubts of your comment. As you are wanting to do, I believe it is not possible.
– gustavox
Gustavo, thanks. I will create another . PHP and send via POST e la monto a minha logica. Thanks.
– mariofel
@mariofel I didn’t understand very well what the end result you want, the behavior you expect from the application. If you are going to present the data on the same page can do only with JS. If you need to write in the comic can do with
ajax
, all without needingpost
, without re-loading the page or opening another one... what you can’t do is to change the value of the PHP variable by the JS if... But blz, if solved then quiet. abs– gustavox