Hello!

I have a page with a product catalog, where before adding a product to the cart, you can specify an arbitrary number of product units, but the maximum quantity of goods that can be added is loaded from the database using PHP at max = "":

<input type="text" value="1" min="1" max="<?=$product['nalichie']?>"> 

Further look, please, an example - here.

I want to do the following:

Write the maximum number of goods in the variable js, which can be added to the cart:

 <script type="text/javascript">var nalichie ='<?=$product['nalichie']?';</script> 

It turns out this line js is displayed for each product, but with its own number. That is, I pass the value of PHP to JS.

Now my variable value is always the same, and not the product on which I clicked to add +1 product, for example. I alert(nalichie); this with alert(nalichie);

Why do I do such a check. After all, in fact, for the input field, and so the maximum number that a person can enter is given and all this is checked using js. But it is worth only in the developer panel to change the maximum number to an arbitrary number of its own, js will take a new value from max = "..." , and I would not want to.

I think this decision is useful to many. Can advise another way. I hope for your help :) I would be very grateful!

  • I did not understand why these dances with a tambourine. At the time of loading the page, a list of products is generated indicating the maximum quantity available for ordering. More can not be selected. When adding to the basket (the second option - when confirming the order), we check with php the correctness of all data. Why complexity? Well, let someone even changes your HTML before stupor. You have nothing to do with it. The main thing is to check everything on the server - using php. - n.osennij
  • @ n.osennij I want this method of verification. In the basket I will apply the same. I decided this way because I plan in the future to check the value of the rest of the goods online (constant updating of the variable), since they can place orders while the person is sitting on the page and thinks how much to order the goods. But this is a separate question. - Dmitry
  • @ Well then, do ajax. When a person enters the number of ordered goods - send a request to php, which checks how much of the goods at the time left, and if the person does not want to order more. - n.osennij
  • @ n.osennij Also true. But how to do it, I do not know. I would have known - I did :) And now, once I opened the question, I want to solve it for myself. I hope for help. - Dmitry

1 answer 1

Based on a small discussion in the comments came to the following option: use ajax. Personally, it seems to me that this is not justified. Anyway.

  1. Connect jQuery to html (connects as js)
  2. Each item in a hidden field must have its own identifier from the database. Desirable value from the indexed field. But the easiest way is to add a product_ID to the value of each button (exactly in your case) - this will be in the example.
  3. With an increase in the number of ordered goods, we grab the ID of the product and send it to the php script for checking using ajax.

 jQuery(".КНОПКА").click(function(event) { // .КНОПКА - класс ΠΊΠ½ΠΎΠΏΠΎΠΊ, Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ отслСТиваСм id=$(this).val(); //Π±Π΅Ρ€Ρ‘ΠΌ value ΠΊΠ½ΠΎΠΏΠΊΠΈ. Π’Π°ΠΌ ΠΌΡ‹ Ρ…Ρ€Π°Π½ΠΈΠΌ Π˜Π” Ρ‚ΠΎΠ²Π°Ρ€Π° aj("handler.php", false, "html", {"id": id}, s_handler); //ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅ Π½Π° false (ΠΌΠΎΠΆΠ½ΠΎ Π½Π° true) - синхронно ΠΈΠ»ΠΈ асинхронно! }); function s_handler(result) { //ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ ΠΎΡ‚Π²Π΅Ρ‚ } function aj(url, async, datatype, data, success) { $.ajax({ type: "POST", url: url, async: async, //timeout: 20000, //ВрСмя оТидания выполнСния запроса 20с. dataType: datatype, data: data, success: success, error: function(err){console.log(err);} }); } 

On the php side, we do all the checks and send some kind of response. Which accepts the same ajax (s_handler function) and does something.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin ♦