PDA

View Full Version : Saving on the server and recalling data



nielse63
09-12-2009, 09:04 PM
I'm about half way through this code, but I'm having trouble saving and recalling the data that I want saved. Right now I'm working on a function where the user clicks on either "Good" or "Bad" and the number next to each word displays the number of times that word has been clicked. I'm having trouble saving the number clicks that each word is getting so that when the page is reloaded, they aren't set back to zero. Here's what I have so far:



<script>
function add_to_goodcount1(){
var goodcount1=document.getElementById('goodcount1');
var cstore = document.getElementById('good_counter_score1');

cstore.value=parseInt(cstore.value)+1;
goodcount1.innerHTML=cstore.value;
}

function add_to_badcount1(){
var badcount1=document.getElementById('badcount1');
var cstore = document.getElementById('bad_counter_score1');

cstore.value=parseInt(cstore.value)+1;
badcount1.innerHTML=cstore.value;
}
</script>

<a href="javascript:add_to_goodcount1();">Good </a><input type="hidden" name="good_counter_score1" id="good_counter_score1" value='0'><span id="goodcount1">0</span>
<p>&nbsp;</p>
<a href="javascript:add_to_badcount1();">Bad </a><input type="hidden" name="bad_counter_score1" id="bad_counter_score1" value='0'><span id="badcount1">0</span>


Any help on how to save this on the server would be very much appreciated. I'm really new to all this, so if you could possibly provide an example of how it should be written I'd appreciate it.

Thanks!

JShor
09-12-2009, 09:55 PM
Use ajax. You'll need to create a new database and user, and then run the sql dump.

goodbad.php


<?php

$count = $_POST['count'];

mysql_connect("localhost", "EDIT_DB_USER_HERE", "EDIT_DB_PASS_HERE");
mysql_select_db("EDIT_DB_NAME_HERE");

mysql_query("UPDATE count SET count='$count'") or die(mysql_error());

?>




<script type="text/javascript">
function ajaxFunction(count) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("POST","goodbad.php",true);
xmlhttp.send(POST);
}

function add_to_goodcount1(){
var goodcount1=document.getElementById('goodcount1');
var cstore = document.getElementById('good_counter_score1');

cstore.value=parseInt(cstore.value)+1;
goodcount1.innerHTML=cstore.value;
ajaxFunction(cstore.value);
}

function add_to_badcount1(){
var badcount1=document.getElementById('badcount1');
var cstore = document.getElementById('bad_counter_score1');

cstore.value=parseInt(cstore.value)+1;
badcount1.innerHTML=cstore.value;
ajaxFunction(cstore.value);
}
</script>

<a href="javascript:add_to_goodcount1();">Good </a><input type="hidden" name="good_counter_score1" id="good_counter_score1" value='0'><span id="goodcount1">0</span>
<p>&nbsp;</p>
<a href="javascript:add_to_badcount1();">Bad </a><input type="hidden" name="bad_counter_score1" id="bad_counter_score1" value='0'><span id="badcount1">0</span>


sql dump:


CREATE TABLE IF NOT EXISTS `count` (
`count` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `count` (`count`) VALUES(0);


HTH:)

nielse63
09-12-2009, 11:14 PM
how do I create the SQL dump?

JShor
09-12-2009, 11:30 PM
Do you have MySQL/PHP support?

JShor
09-12-2009, 11:30 PM
&& if so, do you have phpMyAdmin installed? [This is standard if you have cpanel as your host]