Insert with ajax on onclick

Asked

Viewed 935 times

1

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

Example is button to like in case each click sum +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);
    }})}

  • you have error in javascript or php ? specify better your doubt post your return what you are getting in ajax ? you have already looked at your browser debug if you have any error in chome is just hit the F12 and go on console

  • in fact nothing happens, I do not understand much of ajax I want to know a correct way to carry out this operation

  • jquery.js:9664 POST http://kingofeletro.com.br/3/[Object%20Object] 404 (Not Found)send @jquery.js:9664ajax @jquery.js:9215m.(Anonymous Function) @jquery.js:9361likes @ (index):258onclick @ (index):270 13owl.carousel.min.js:1 Uncaught Typeerror: Cannot read Property 'clone' of undefinedrun @ Owl.carousel.min.js:1e @jquery.js:548e.update @Owl.carousel.min.js:1(Anonymous Function) @Owl.carousel.min.js:1e jquery.js:548

  • Now it is working but it is not sending the id of the record in onclick Function like(id,identify){ $. ajax({ url:("ajax/like.php"), type: "POST", date: "udid="+id, Success:Function(data){ $("#"+identify). html(data); }})} echo "<button id='$guidcurti' role='button' onClick='curti($guid,$guidcurti)' class='btn btn-default fa fa-Thumbs-o-up'>($like)</button>";

2 answers

0

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

-1


I managed to solve, I’ll leave the method in case someone is looking for how to do

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 there is a way to improve a lot, who wants to edit the will.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.