-2
I have a quantity variable, and I have two tags <a>
who function as onclick
and call the function myFunc1
or myFunc2
. One serves to increase the amount and the other to decrease. myFunc1 to decrease and myFunc2 to increase.
<a onclick='myFunc1()' style='font-size:18px' id='diminuir' class='fa'></a>
<script type="text/javascript">
function myFunc1() {
var oldValue = <?php echo json_encode($quantidade)?>;
if (oldValue > 0) {
var newVal = parseFloat(oldValue) - 1;
} else {
newVal = 0;
}
<?php echo json_encode($quantidade)?>; = newVal;
}
function myFunc2() {
var oldValue = <?php echo json_encode($quantidade)?>;
var newVal = parseFloat(oldValue) + 1;
<?php echo json_encode($quantidade)?> = newVal;
<?php echo json_encode($quantidade)?>; = newVal;
}
</script>
I want the tags to increase and decrease the variable amount, and from what I saw the only way I saw to do this is with onclick, and to make the function I want to pass the variable amount that is php, increase it with javascript and convert it back to php
But this code is not working, the problem is the differentiation of variables, the tag or anything else?
Explain in words what you tried to do there, because the code made no sense.
– Woss
If you are on a php page, the code
var oldValue = <?php echo json_encode($quantidade)?>
will work, but this other code does not make sense<?php echo json_encode($quantidade)?> = newVal
. Have you thought about what will appear in the middle of the codejavascript
? something like1 == newVal
, and what it would do?– Ricardo Pontual
@Andersoncarloswoss I’ve edited there
– edshewa
But do you want to increase/decrease the value in JS and play it in the PHP variable? That’s not possible. Read on frontend and backend to better understand. One runs on the client side; the other on the server. The only way they can communicate is via messages via a communication protocol (HTTP, in general). If you want to do JS converse with PHP, use AJAX.
– Woss
I want the <a> tags to increase and decrease the variable amount, and from what I saw the only way I saw to do this is with onclick, and to make the function I want to pass the variable amount that is php, increase it with javascript and convert it back to php
– edshewa
Are you showing the amount somewhere on the page? As Anderson Carlos Woss said, to pass the new amount to PHP you have to send that value to the server, and for that you can use AJAX. If you want to visually change the amount on the page, this is possible with Javascript only, but you will have to send it to the server at some point for PHP to know this new value.
– Leite
@Exact yes milk I want to change the value of the variable, not just visually
– edshewa
edshewa, what you want is not possible this way. To increase the value of your variable in PHP you need to make a request to the server (PHP) which will be responsible for increasing this value.
– Lucas Brogni
You’ll have to follow Anderson Carlos Woss' suggestion then. Or if you want suggestions on which way to go, you need to put more information in the question. You can figure out what you want to do, and with the code you have, it’s not possible. But in more detail someone can help you find the right path.
– Leite
Another thing for you to study: HTTP does not retain status (is stateless). So, what will PHP do with the new value?
– Woss
As an example, when you are looking at a shopping cart you have the items and quantity. Sometimes you can change this amount, and sometimes also with buttons, but for later the server know the new value, there is a button that will submit these new amounts to the server. For example, the fields like this, https://en.helpforsite.com/image/cache/catalog/products/ajax_quantity_for_journal2_cart-1280x1280.png and then there would be a button that you didn’t see there to send to the server.
– Leite
@Milk like a button you can’t see?
– edshewa
It is not seen in the link image, but it would be visible to those who see the page. This image is just an example.
– Leite
Possible duplicate of Match php variable to javascript variable
– Victor Stafusa
Another question in the same style: https://answall.com/q/28673/132
– Victor Stafusa