Ajax最喜欢/不喜欢的按钮

时间:2015-06-14 15:40:54

标签: php jquery ajax

我试图在喜欢/不喜欢的按钮上实现一些Ajax。它背后的想法是,当你点击它时,一个星形变为黄色,当你再次点击它时,它会变回灰色等等。它添加和删除数据库中的数据。

之前我喜欢这个

//imports

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.swing.JFormattedTextField.AbstractFormatter;

import org.jdatepicker.*;
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;

------------------------------------------------------------------------
//constructor

UtilDateModel model = new UtilDateModel();
    Properties p = new Properties();
    p.put("text.today", "today");
    p.put("text.month", "month");
    p.put("text.year", "year");
    JDatePanelImpl datePanel = new JDatePanelImpl(model,p);
    JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
    datePicker.setBounds(230, 30, 110, 30);
    add(datePicker);

------------------------------------------------------------------------
//dateformater

public class DateLabelFormatter extends AbstractFormatter
    {

        private String datePattern = "yyyy-MM-dd";
        private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

        @Override
        public Object stringToValue(String text) throws ParseException
        {
            return dateFormatter.parseObject(text);
        }

        @Override
        public String valueToString(Object value) throws ParseException
        {
            if (value != null)
            {
                Calendar cal = (Calendar) value;
                return dateFormatter.format(cal.getTime());
            }

            return "";
        }

    }

该字段是一个php回声表单

    <?php

include("classes/event.class.php");

$m = new Event();
$arrayAllEvents = $m->getNonFavo();
$arrayFavorites = $m->getFavo();

  $db = new db();

while ($row = mysqli_fetch_assoc($arrayFavorites))
{
  $unfavoriteID = $row['f_id'];
  $uid = $_SESSION['u_id'];
}

if(isset($_POST['favorite_row'])) 
{
  $uid = $_SESSION['u_id'];
   $Fid = $_POST['id_to_be_favo'];
   if(!mysqli_query($db->conn, "INSERT INTO favorites (u_id, n_id, f_boolean) VALUES
        ('". $db->conn->real_escape_string($uid) ."' ,
        '". $db->conn->real_escape_string($Fid) ."' ,
        '". $db->conn->real_escape_string("1") ."')"))
   {
     echo mysqli_error($db->conn);
   }
}

if(isset($_POST['Unfavorite_row'])) 
{
  $unFid = $_POST['id_to_be_unfavo'];
    if(!mysqli_query($db->conn, "DELETE FROM favorites WHERE f_id ='".$unFid."'"))
    {
      echo mysqli_error($db);
    }
  }
?>

我试图将表单更改为按钮并使用jQuery / Ajax进行切换。 我一直在尝试一些我在网上找到的不同解决方案,但似乎都没有。

实现这一目标的最佳方式是什么?

亲切的问候

1 个答案:

答案 0 :(得分:0)

<input type='button' class='favorite' id='<?php echo $a['n_id']; ?>' value='favorite' />

<div class='clearfix'> </div>";       
使用

$(function() {
  $(".favorite").on("click".function() {
    $.post("whatever.php", {"favorite_row":this.id}, function(data) {
      $("#star").html('<img/>',{"src":data.fave==yes"?"true.png":"false.png"});
   }); 
});

现在返回{“fave”:“yes”}如果被收藏