PHP Javascript - IF error

Asked

Viewed 105 times

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>

1 answer

3

Basically the problem is that it is not possible to determine the value of the PHP variable $minhavar with the if javascript.

When I went to check the page’s view-souce it already runs all the <?php and the last always prevails.

This is because PHP (which is server side) does not interpret (does not execute commands) what is outside tags <?php ?>, or <? ?> (and what is inside comments).

For PHP, everything outside these tags (or inside comments) simply "does not exist" (like commands). All this if/else Javascript is just "any text" that will be sent to the browser without being changed by the PHP interpreter.

This is the code that PHP sent to the render browser:

function myFunction() {

            var item = form1.varhtml.value;

            if (item == 1) {
                alert("1200");
            }
            else if (item == 2){
                alert("2200") ;
            }
            else if (item == 3){
                alert("3200") ;
            }

        }

Exactly what you had him do when you said $minhavar value x ($minhavar = 1.200 ex.) and gave a echo (have written) right away.

Who will interpret what is inside the tags <script> </script> is the browser (client side). Here’s a good question with some great answers about the difference between server side and client side: What’s the difference between client-side and server-side code in web development?

In short, it is not possible to change the value of a PHP variable due to a condition of a Javascript code, because the first one runs on the server (and does not understand the tags <script>) and the second in the customer.

In fact, even in the original code the first alert is always according to what you selected, and only the last one (at the end of the function) that always appears with 3,200.

When you do:

 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; ?>") ;
    }

PHP will simply be redeclareting the variable $minhavar with another value (first 1,200, then 2,200, and finally 3,200).

To obtain the expected result (i.e., for the alert appear with the expected value), just remove the last alert(), and replace the last two ifs for else ifs.

But notice that the value of the variable, at the end of the script, will always be 3,200, no matter the value you select in JS.

<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; ?>") ;
        }

    }

</script>

<?php echo $minhavar; ?> // saída é sempre R$ 3.200
  • 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.

  • 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 I edited the answer trying to clarify the doubts of your comment. As you are wanting to do, I believe it is not possible.

  • Gustavo, thanks. I will create another . PHP and send via POST e la monto a minha logica. Thanks.

  • @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 needing post, 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

Browser other questions tagged

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