PHP MYSQL之类的按钮 [英] PHP MYSQL like button

查看:107
本文介绍了PHP MYSQL之类的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的评论供稿做了一个类似的按钮,但是每次单击它都会重新加载页面,所以我想使其不重新加载页面.我在任何地方都找不到我的问题的答案.我了解我将不得不使用javascript或AJAX,但由于我不知道如何编码,因此我陷入了困境.

Hi i have made a like button for a commenting feed that i have but every time i click it it reloads the page i would like to make it so that it dose not reload the page. i cant find the answer to my question anywhere. i understand that i would have to use javascript or AJAX but as i do not know how to code in that i am stuck.

这是我的评论供稿所在的页面.页面的名称是member-index.php

This is on the page where my commenting feeds are. the name of the page is member-index.php

<a href=\"like-exec.php?id=".$rows['ID']."&members_id=".$_SESSION['SESS_MEMBER_ID']."\">like</a>

这是在执行代码的页面上(例如-exec.php)

and this is on the page that executes the code (like-exec.php)

<?php
require('../config/connect.php');
require('../config/config.php');

$sql = "UPDATE comments set `like` = `like`+1 where `ID` = '$_GET[id]'";
$result=mysql_query($sql);


$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("likes", $con);

mysql_query("INSERT INTO likes (members_id, comm_id) VALUES(".$_SESSION['SESS_MEMBER_ID'].", $id)");


mysql_close($con);
header("location: success.php");

?>

该代码完成后,将其发送到susses.php,然后将其重定向到member-index.php.

after that code has finished it gets sent to susses.php that then redirects it to member-index.php.

推荐答案

PHP/HTML

在您的喜欢"链接中添加一个类,以便我们可以在jQuery中更轻松地定位它,并使链接的ID成为行ID.还值得检查一下页面上其他位置是否没有数字ID,因为这些将是简单的ID,可以在标记中的其他位置轻松使用.

Add a class to your 'like' links so that we can target it easier in jQuery and make the id of the link the row id. It's also worth checking you don't have numerical id's elsewhere on the page since these are going to be simple id's that are easily used elsewhere in your markup.

<a class="like" id="<?php echo $rows['ID']; ?>" href=\"like-exec.php?id=".$rows['ID']."&members_id=".$_SESSION['SESS_MEMBER_ID']."\">like</a>

jQuery

$('a.like').click(function() {
    // triggered when like link is clicked

    // get the id of this link
    var id = $(this).attr('id');

    // make the AJAX request to the PHP script that updates the database table

    $.ajax({
        type: "GET",
        url: update_likes.php,
        dataType: 'html',
        data: ({ id: id }), // first id is the name, second is the actual id variable we just created
        beforeSend: function(data) {
            // you can do stuff in here before you send the data, display spinner gif etc
            alert('sending!');
        },
        success: function(data) {
            // same here but when the ajax request is successful
            // the data variable is coming from the echo of your PHP script
            alert(data);
        },
        complete: function(data) {
            // yet again but on completion
            alert('complete!');
        }

    });

    // stops the browser following the link (href) in the a tag
    return false;

});

PHP

我们将ajax请求发送到的新脚本,但这只是您在问题中已经使用的相同代码.

A new script that we send the ajax request to but it's just the same code you already have in your question.

update_likes.php

<?php
require('../config/connect.php');
require('../config/config.php');

// not sure if session_start(); is in your config but you will need it
// in this script somewhere to do your second query.

// $_GET['id'] is now coming via ajax

$sql = "UPDATE comments set `like` = `like`+1 where `ID` = '$_GET[id]'";
$result=mysql_query($sql);


$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("likes", $con);

mysql_query("INSERT INTO likes (members_id, comm_id) VALUES(".$_SESSION['SESS_MEMBER_ID'].", $id)");


mysql_close($con);

// header("location: success.php");
// don't need this anymore

// only echo one string from an ajax request so if you need more do some
// concatenation
echo 'successfully updated db!';

?>

最后一点,由于不建议使用mysql_函数,因此请查看PDO或msqli.

On a final note, the mysql_ functions are deprecated so look into PDO or msqli.

P.S.我尚未测试代码,但希望它应该可以正常工作.

P.S. I haven't tested the code but hopefully it should just work.

更新

尝试将点击功能更改为此:

Try changing the click function to this:

$('a.like').click(function(e) {
    // triggered when like link is clicked


    // stops the browser following the link (href) in the a tag
    e.preventDefault();

    // get the id of this link
    var id = $(this).attr('id');

    // make the AJAX request to the PHP script that updates the database table

    $.ajax({
        type: "GET",
        url: update_likes.php,
        dataType: 'html',
        data: ({ id: id }), // first id is the name, second is the actual id variable we just created
        beforeSend: function(data) {
            // you can do stuff in here before you send the data, display spinner gif etc
            alert('sending!');
        },
        success: function(data) {
            // same here but when the ajax request is successful
            // the data variable is coming from the echo of your PHP script
            alert(data);
        },
        complete: function(data) {
            // yet again but on completion
            alert('complete!');
        }

    });
});

这篇关于PHP MYSQL之类的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆