Insert with ajax in onclick

1

I would like to make an insert from onclick with ajax and already bring the result to the text of this button.

Exemplifying is a button to enjoy in case each click adds +1;

function curti(id,identify){
var base = 'idd='+id;
$.post({
    url:("ajax/curtir.php"),
    dataType:"json",
    data: base,
    success:function(dados){
        $("#"+identify).html(dados[index].curtir);
    }})}

    
asked by anonymous 24.06.2016 / 22:19

2 answers

-1

I was able to solve it, I will leave the method if someone is looking for how to do it

Javascript / Ajax function

function curti(codigo,identify){
$.ajax({
    url:("ajax/curtir.php"),
    type: "POST",
    data: {"udid": codigo},
    success:function(data){
        $("#"+identify).html("("+data+")");
    }})}

PHP function

<?php
   header('content-type: application/json; charset=utf-8');
   include('../class/mysql_crud.php');
   $id = (isset($_POST['udid'])) ? (int)$_POST['udid'] : 0;
   $db = new Database();
   $db->connect();
   $db->sql("SELECT * FROM musicas_home WHERE guid = $id");
   $res = $db->getResult();
   foreach($res as $output){
     $curtirv = $output["curtir"];
   }
  $curtir = ($curtirv + 1);
  $db->update('musicas_home',array('curtir'=>$curtir),'guid='.$id);

  echo json_encode($curtir);

Button calling function with onclick

echo "<button id='$guidcurti' role='button' onClick='curti($guid,$guidcurti)' class='btn btn-default fa fa-thumbs-o-up'>($curtir)</button>";

Surely you can improve a lot, whoever wants to edit at will.

    
27.06.2016 / 15:02
0

Do you think you need to return a Json? You could simply put a $ ("#" + identify) .html (data) and in the file that does your function you gave echo in the html. Example: echo "Success. This span would already appear in your main html.

    
25.06.2016 / 17:37