Файл js:
function favorite(id) {
jQuery.ajax({
url: "favorite.php",
method: "POST",
data: { id: id }
});
}
php:
$id = text($_POST['id']);
if ($id){
$q = DB::$dbs->prepare("SELECT * FROM `favorite` WHERE `stantion_id` = ? AND `user` = ? LIMIT 1");
$q->execute([$id,App::user()->id]);
if (!$no = $q->fetchObject()) {
$q = DB::$dbs->prepare("INSERT INTO `favorite` (`stantion_id`,`user`) VALUES (?,?)");
$q->execute([$id,App::user()->id]);
$ok = 'add';
}
else
{
DB::$dbs->query("DELETE FROM `favorite` where `stantion_id` = ? AND `user` = ?",array($id,App::user()->id));
$ok = 'del';
}
}
$out = array(
'ok' => $ok,
);
header('Content-Type: text/json; charset=utf-8');
echo json_encode($out);
html:
<button onclick="favorite('<?= $id ?>'); return false;">В избранное</button>
После клику по кнопке, нужно заменить надпись на кнопке
<button onclick="favorite('<?= $id ?>'); return false;">Удалить с избранного</button>
Как это сделать?