?yb? 2011-03-05 00:53 采纳率: 100%
浏览 32

Ajax + jQuery + PHP + SQL支持

Hey, I'm new to PHP And Ajax. I'm pretty good with SQL Though. I was wondering how I could an image, and when you click on it, run an ajax request to run a sql query

mysql_query("INSERT INTO `favorites` (`uid`, `gid`, `added`) VALUES ($member, $id, '$datetime');");

and update the image to a delete from favorites button.

using session for the user id (uid) and gid from index.php?id=13

http://davidknag.com/fav.png

to

http://davidknag.com/unfav.png

when clicking the unfavorite button delete that entry from sql.

I have never used ajax before and rarely used javascript.

  • 写回答

2条回答 默认 最新

  • weixin_33712987 2011-03-05 00:58
    关注

    So ajax with jQuery on image class click....

    window.onload = function(){
       $('.image').click(function(){
        var image_id = $(this).attr('id');
        $.ajax({
            type: "POST",
            url: "/ajaxpage.php",
            data: {
                image_id:image_id
            }
            success: function(data){
                alert(data);
                $('#'+image_id).remove().appendTo('#removed');
            },
            failure: function(){
                alert('failed');
            }
        });
       });
     }
    

    In your php file, you can take the necessary data (the id of the image or whatever) from the $_POST variable and execute your query. Echo some sort of string as a response, and then in the success handler of your ajax call append appropriately. Speak up if you run into difficulty, or have specific questions.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog