Erwin van Ekeren
Erwin van Ekeren

Reputation: 730

store time from stopwatch Javascript in MySQL database

I am using a javascript to start and stop a timer (stopwatch). What I want is that when the user press the stop button, the data (the time at that moment) will be stored in my MySQL database. Is that possible? And if yes, what do I need to add as code? (I'm just a starter with PHP, if someone can add a small explanation then that would be great so I can learn from it)

The following script is my working javascript code:

var clsStopwatch = function () {

var startAt = 0;
var lapTime = 0;

var now = function () {
    return (new Date()).getTime();
};

this.start = function () {
    startAt = startAt ? startAt : now();
};

this.stop = function () {
    lapTime = startAt ? lapTime + now() - startAt : lapTime;
    startAt = 0;
};

this.time = function () {
    return lapTime + (startAt ? now() - startAt : 0);
};
};

var x = new clsStopwatch();
var $time;
var clocktimer;

function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}

function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';

m = Math.floor(time / (60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time / 1000);
ms = time % 1000;

newTime = pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 2);
return newTime;
}

function show() {
$time = document.getElementById('time');
update();
}

function update() {
$time.innerHTML = formatTime(x.time());
}

function start() {
clocktimer = setInterval("update()", 1);
x.start();
}

function stop() {
x.stop();
clearInterval(clocktimer);
}

The next is my PHP code that I use to store data in my database, it's on a separate page.

define('DB_NAME', 'dbname');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'host');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) { die('could not connect: ' . mysql_error()); }

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) { die('can not use ' . DB_NAME . ': ' . mysql_error()); }

$sql="INSERT INTO game (name, company, email, time) VALUES
    ('".$_POST['name']."','".$_POST['company']."','".$_POST['email']."','".$_POST['time']."')";

if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }

mysql_close();

and my HTML submit form:

<body onload="show();">

<form action="highscore.php" method="post"/>

name:<br/>
<input type="text" name="name"/><br/>

company:<br/>
<input type="text" name="company"/><br/>

email:<br/>
<input type="text" name="email"/><br/>

Time: <span id="time"></span><br/>
<!--<input type="text" name-"time">-->
<input type="button" value="start" onclick="start();">
<input type="submit" value="stop" onclick="stop();">
<br/><br/>

</form>

</body>

Maybe a solution could be to post the data in a hidden input field but still i'm struggling to get that one working.

Upvotes: 0

Views: 3124

Answers (2)

Hackerman
Hackerman

Reputation: 12295

Try this:

function stop() {
x.stop();
document.getElementById('counter').value = formatTime(x.time());
clearInterval(clocktimer);
}

On your form:

<input type="hidden" value="" id="counter" name="counter" />

Upvotes: 1

Use MySQLi instead of MySQL, because there's some serious security problems with MySQL

Upvotes: 0

Related Questions