-1

I need it to fit and I click on a file to increase the number to 1 and saved in the database

I already have the database created and part of the AJAX code ready, in addition to the number YA INCREMENTA, the issue is that I have an update of the page manually instead of only updating the div

Number to update

<span id="'.$rowRE[id_reclamo].'" class="badge badge-primary badge-pill like">'.$rowRE[positivo].'</span>

Function with ajax

  $(document).ready(function () {
    $('.like').on('click', function () {
        var reclamoID = $(this).attr('id');
        if (reclamoID) {
            $.ajax({
                type: 'POST'
                , url: 'like.php'
                , data: 'reclamo_id=' + reclamoID
                , success: function () {}
            });
        }
        else {
            alert("error");
        }
    });
});

php code

  $reclamoID=$_POST['reclamo_id'];
$query = $db->query("UPDATE reclamos SET positivo = positivo +1 WHERE id_reclamo = $reclamoID");

//Count total number of rows $rowCountTabla = $query->num_rows;

I need you NOT to recharge the entire page if not ONLY THE NUMBER

  • **WARNING**: Whenever possible use **prepared statements** to avoid injecting arbitrary data in your queries and creating [SQL injection bugs](http://bobby-tables.com/). These are quite straightforward to do in [`mysqli`](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [PDO](http://php.net/manual/en/pdo.prepared-statements.php) where any user-supplied data is specified with a `?` or `:name` indicator that’s later populated using `bind_param` or `execute` depending on which one you’re using. – tadman Jan 18 '19 at 02:16
  • If you want it to do something, add some JavaScript code to the `success` function. Right now that's empty, so it does nothing. – tadman Jan 18 '19 at 02:17
  • Thanks for the answer, I just want you to update in this case the "span" with the new value, that is, the incremented value – Tomas Francisco Firbeda Jan 18 '19 at 02:18
  • I'm giving you a suggestion here. I'm not volunteering to do the work. Read up on how jQuery works, changing values with `html()` is super easy. – tadman Jan 18 '19 at 02:19
  • ok, thanks and good luck – Tomas Francisco Firbeda Jan 18 '19 at 02:20

1 Answers1

0

Return the count in the same request you make when you post your data. Something along the lines of:

PHP:

$reclamoID = pg_escape_string($_POST['reclamo_id']);

$results = $db->query("SELECT positivo FROM reclamos WHERE id_reclamo = '".$reclamoID."'");
// Whatever DB wrapper you're using here... this is just a guess.
$count = $results[0]['positivo'];

echo json_encode(array(
  'id' => $reclamoID,
  'count' => $count
));

Javascript:

$('.like').on('click', function () {
  var element = $(this);
  var reclamoID = element.attr('id');
  if (reclamoID) {
    $.post(
      'like.php',
      {
        reclamo_id: reclamoID
      },
      function (responseData) {
        element.text(responseData.count);
      },
      'json'
    );
  }
});

ALWAYS sanitize posted data, to prevent injections and other malicious code.

Cue
  • 2,699
  • 1
  • 12
  • 13
  • Thanks for your answer, the js code throws me the following error: Without capturing SyntaxError: missing) after the argument list – Tomas Francisco Firbeda Jan 18 '19 at 02:38
  • Why are you calling a Postgres escaping function when inserting with MySQL? It's good you're raising concerns with bold text, but the real problem here is not using prepared statements with placeholder values. – tadman Jan 18 '19 at 16:59