• PHP/AJAX script that updates a mysql table when a button is clicked
    6 replies, posted
Hi, I'm trying to make a system whereby a user clicks a button and their score on a mysql table increases by 1, as does their score on a scoremeter on the webpage. How would I do this? [php]<?php $con = mysql_connect("fdb2.leadhoster.com", "xxxxx", "xxxxx"); mysql_select_db("516980_darknesz"); $result = mysql_query("SELECT * FROM count"); (int)$count = mysql_result($result, 0, count); session_start(); if(!isset($_SESSION['key'])) { $_SESSION['key'] = md5(rand(1000000, 9999999)); } if($_GET['do'] == 'update') { $count++; $result = mysql_query("UPDATE count SET count='$count'"); $_SESSION['key'] = md5(rand(1000000, 9999999)); die($_SESSION['key']); } ?>[/php] [php]<script type="text/javascript"> $(document).ready(function() { var count = <?=$count;?>; var key = '<?=$_SESSION['key'];?>'; $('#update').click(function() { $.ajax({ type: "POST", url: "click.php?do=update", data: "key=" + key, success: function(rtn) { key = rtn; } }); count++; $('#count').text(count); }); }); </script>[/php] When the button with the id "update" is clicked, the AJAX is supposed to run, and if the php returns successful, the div with the id "count" should have its value increased by 1. This doesn't seem to work though. Any ideas? Also, here's the full code for the page: [url]http://pastie.org/1306535[/url] Thanks
WHy die? just echo it?
[QUOTE=commander204;26123428]WHy die? just echo it?[/QUOTE] Yeah, that was just for my purposes as it was being developed, will change it when it's finished. Do you know how I can make the script do what I want it to?
[QUOTE=commander204;26123428]WHy die? just echo it?[/QUOTE] It's a good practice, if he were to add code underneath that it could possibly run and affect output.
[QUOTE=compwhizii;26126654]It's a good practice, if he were to add code underneath that it could possibly run and affect output.[/QUOTE] Thank you Also, please help me compwhizii
$('#update').click(function() should be $("#update").click(function() Or does it not matter, i am not very good at javascript/jquery.
[QUOTE=Pocoyo;26131443]$('#update').click(function() should be $("#update").click(function() Or does it not matter, i am not very good at javascript/jquery.[/QUOTE] The type of quotes doesn't matter, I almost always find myself using single quotes when dealing with Javascript. OP, I notice that you are including jQuery twice, the second time appears to override the handles set before the library was loaded the second time. Remove the second inclusion of jQuery, and try out the script.
Sorry, you need to Log In to post a reply to this thread.