Reputation: 175
Here is my javascript code
<script>
$(document).ready(function(){
$(".five").click(function(){
<?php echo updatepoints();?>
});
});
</script>
Here is my php code
<?php
function updatepoints() {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'");
$row=mysql_fetch_array($query);
$points = $row["points"];
$points = $points + 5;
mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'");
}
?>
Both these codes are on same page one below other. This does not seem to update the values. How should I proceed?
Upvotes: 0
Views: 90
Reputation: 45490
You can use jQuery, it makes things simpler:
<script>
$(document).ready(function(){
$.post("update_user_points.php", { username: 'someuser' }, function(response) {
console.log(response);//check your console
});
});
</script>
I didn't see how you start the session, but this example passes the username
value from the client to PHP. if you just incrementing the points by 5 you can do it in a better way check this:
<?php
if(isset($_POST['username']){
function updatepoints($username) {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = sprintf("UPDATE member set points = points + 5 WHERE username='%s'",
mysql_real_escape_string($username));
// Perform Query
$result = mysql_query($query);
// Check result
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}else{
echo 'update success';
}
}
//call the function
updatepoints($_POST['username']);
}
?>
Upvotes: 1
Reputation: 74216
Here, try this:
Sidenote: content.php
will be your SQL file, which is being called by xmlhttp.open("GET","content.php",true);
.
This will work, assuming your SQL is already good.
<!doctype html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","content.php",true);
xmlhttp.send();
}
</script>
</head>
</head>
<body>
<script>
setInterval(loadXMLDoc,5000);
</script>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
Upvotes: 2
Reputation: 2069
Create a file called ajax_update.php . Inside that file add your function and user ajax to call that function.
ajax_update.php
<?php
function updatepoints() {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'");
$row=mysql_fetch_array($query);
$points = $row["points"];
$points = $points + 5;
mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'");
}
if($_POST['action_type'] == 'update') {
updatepoints();
}
?>
change your script like this
<script>
$(document).ready(function(){
$(".five").click(function(){
$.ajax({
type : "POST",
url : "ajax_update.php",
data : {action_type:"update"},
success : function(html){
}
});
});
});
</script>
You can change your ajax PHP file as separate file for ajax and class. Add functions in class. Let it be neat and clear
Upvotes: 1