V
V
V012020-05-05 13:08:13
PHP
V01, 2020-05-05 13:08:13

Can this code be translated into PHP?

$('.minus-btn').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var $input = $this.closest('div'). find('input');
var value = parseInt($input.val());

if (value > 1) {
value = value - 1;
} else {
value = 0;
}

$input.val(value);

} );

$('.plus-btn').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var $input = $this.closest('div'). find('input');
var value = parseInt($input.val());

if (value < 100) {
value = value + 1;
} else {
value =100;
}

$input.val(value);
});

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Valentin Birulya, 2020-05-05
@nykakdelishki

Yes. You can.
Or do you want someone to write it for you?

A
AUser0, 2020-05-05
@AUser0

The JavaScript programming language and the PHP programming language are completely different things. And it doesn't make sense to translate this code into PHP.
If you give analogies, then your question can be rephrased as follows: is it possible to cut hair with a sausage?
Nonsensical question, right? Or: is it possible to fill a car with a scarf? What nonsense, you ask. And here's another: is it possible to feed a child with an umbrella?
That's your question - it doesn't make sense. What is done in JavaScript (the given code) is done in JavaScript because it was invented for this work. If it’s still not clear, you should contact a specialist who will chew it again and again, in other words, until you fully understand ...

S
Sergey Kupletsky, 2020-05-05
@zavoloklom

PHP is a server-side language, and in the above code, the user clicks on the buttons on the site in the browser are processed.
Therefore, no.

T
ThunderCat, 2020-05-05
@ThunderCat

This... Can you tell me the meaning of such a transformation?
V01 UPD: In the end, everything is bad, but:
if the task is to implement this ... the division is without zhs "at any cost" - make the buttons links to a script like cart.php, add in the get parameters what needs to be done and with what, for example cart.php?item_id=666&action=plus, in this file, already in the session, write the product id and quantity from the get parameters, you can even use the simplest array id-quantity (such as this [666=>1,667=>3]), and redirect back to the page where you came from. On each page in the header, you take out your "basket" from the session and display what you need there. You can do the same with a form, where your + and - will be submit buttons and the form will be sent by the post method, the essence and logic is about the same.

N
Nazawrius, 2020-05-06
@Nazawrius

JavaScript, in this case jQuery, is used to implement the frontend. You will not be able to implement page element management in PHP, simply because this language is backend development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question