PHP/HTML
는 '같은'링크에 클래스를 추가합니다. 이 코드는 마크 업의 다른 곳에서 쉽게 사용할 수있는 간단한 ID 일 것이므로 페이지의 다른 위치에있는 수치 ID를 가지고 있지 않은지 확인하는 것도 가치가 있습니다.
<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
우리는하지만 그것은 단지 동일한 코드가 이미 귀하의 질문에이있어하는 아약스 요청을 보내 새로운 스크립트.
마지막 주에
<?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!';
?>
update_likes.php는 mysql_
기능은 PDO 또는 msqli로 보면 그렇게되지 않습니다.
P. 코드를 테스트하지는 않았지만 잘하면 그냥 작동해야합니다. 여기에 클릭 기능을 변경
UPDATE
시도 :
$('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!');
}
});
});
그 중 하나만 남았습니다. 반환을 제외한 모든 것이 작동합니다. false; 일부는 여전히 링크를 따라 간다. 왜 어떤 생각? –
흠, 그렇게해서는 안되지만 링크의 href를 꺼내고 href 속성으로'#'을 사용하십시오. 브라우저가 여전히 링크를 따라 간다면 최악의 경우 스크롤하여 상단으로 스크롤합니다. 페이지의 – martincarlin87
나는 시도했다. 내가 MAMP를 사용하고 있다는 사실이 될 수 있습니까? 곧 웹 서버로 업로드하여 곧 작동 할 것이라고 기대하고 있습니다. –