Hello!

You must pass two variables without reloading the page when clicking on an item. At the moment, the code looks like this:

var id = "<?php echo $row["id"];?>"; var count_vk = "1"; var count_fb = "1"; var count_tw = "1"; $(document).ready( function() { $("#share_wrap_vk").click(function(){ $.ajax({ type: 'POST', url: '/blocks/share_counter.php', data: "" }); }); $("#share_wrap_fb").click(function(){ $.ajax({ type: 'POST', url: '/blocks/share_counter.php', data: "" }); }); $("#share_wrap_tw").click(function(){ $.ajax({ type: 'POST', url: '/blocks/share_counter.php', data: "" }); }); }); 

You need to transfer the id and count_vk to the share_counter.php document. count_vk, fb, tw are needed only to understand which field to update in the database table. I get the id when loading the page from the database. The essence of the script is to count the number of clicks on a link with a specific identifier.

  • @Torawhite, to unify languages ​​is a bad idea. The variety of types is the only thing that saves us from extinction, plus it’s fun, everything is different :) Even if you had to unite languages, it’s better in JS, when you get to know him better, you’ll understand that he is much better than PHP This is how to compare carrots and aircraft). - user31688
  • @Torawhite, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

2 answers 2

In the ajax request in the data field, pass the object.

Update

 var id = "<?php echo $row["id"];?>"; var count_vk = "1"; var count_fb = "1"; var count_tw = "1"; var wrapAjax = function(soc){ $.ajax({ type: 'POST', url: '/blocks/share_counter.php', data: { count: soc } }); }; $(document).ready( function() { var $document = $(document); $document.on('click',"#share_wrap_vk",wrapAjax(count_vk)); $document.on('click',"#share_wrap_fb",wrapAjax(count_fb)); $document.on('click',"#share_wrap_tw",wrapAjax(count_tw)); }); 
  • in general, it would be better to write code - Lgunchik
  • This is clear, I do not quite understand the syntax. I mean, I can’t just rewrite the variables that I need to separate by commas. It’s not yet clear whether this kind of work will work: var id = "<? Php echo $ row [" id "];?>"; That is, does the desired value and the variable itself fall into a function below? - Torawhite
  • @Torawhite, if $row['id'] is a number, then quotes are not needed: var id = <? print $row['id']; ?>; var id = <? print $row['id']; ?>; For the future - divide the model and the view, write one in another badly, you will get confused. - user31688

Bydlokod is, and the concept is something like this:

 // File: Some.js $(function(){ $('#your_button').on('click', function(){ $.post('target.php', { id: 'your_variable_with_id_here' }); }); }); <? // File: target.php $id = $_POST['id']; 
  • It is yes, but I doubt that at the beginning of a career, FrontEnd, IMHO, should be tied. - user31688
  • @TheDoctor I do not make any programming career, I only make a website for myself, that's all. - Torawhite
  • I wrote that in place of " 'your_variable_with_id_here' " you need to put your ID from JS, if it is in the id variable, then this: id: id :) - user31688
  • @TheDoctor That is id: id, count_vk: 1? Otherwise, the handler will not understand which field needs to be updated (vk, fb, tw) I have no doubt that it can be done more competently, but I do not know how. An array with two id variables and one of the count variables are supplied to the handler. In it, the code if $ _POST ['count_vk'] exists, then update one cell, if $ _POST [count_fb '] exists, then update the second cell and also with tw - Torawhite
  • It can be solved something like this: <? $ id = $ _POST ['id']; // Get the ID from the incoming array if ($ _ POST ['count_vk']) // Update the contact fields if ($ _ POST ['count_fb']) // Update the FBI fields, i.e. Facebook if ($ _ POST ['count_tw']) // Update tweakster fields // Etc ... - user31688