Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  159] [ 2]  / answers: 1 / hits: 132436  / 13 Years ago, wed, december 7, 2011, 12:00:00

I have this little click counter. I would like to include each click in a mysql table. Can anybody help?



var count1 = 0;
function countClicks1() {
count1 = count1 + 1;
document.getElementById(p1).innerHTML = count1;
}


document.write('<p>');
document.write('<a href=javascript:countClicks1();>Count</a>');
document.write('</p>');

document.write('<p id=p1>0</p>');


Just in case anybody wants to see what I did:



var count1 = 0;
function countClicks1() {
count1 = count1 + 1;
document.getElementById(p1).innerHTML = count1;
}
function doAjax()
$.ajax({
type: POST,
url: phpfile.php,
data: name=name&location=location,
success: function(msg){
alert( Data Saved: + msg );
}
});
}

document.write('</p>');
document.write('<a href=javascript:countClicks1(); doAjax();>Count</a>');
document.write('</p>');
document.write('<p id=p1>0</p>');


This is phpfile.php which for testing purposes writes the data to a txt file



<?php
$name = $_POST['name'];
$location = $_POST['location'];
$myFile = test.txt;
$fh = fopen($myFile, 'w') or die(can't open file);
fwrite($fh, $name);
fwrite($fh, $location);
fclose($fh);
?>

More From » mysql

 Answers
21

JavaScript, as defined in your question, can't directly work with MySql. This is because it isn't running on the same computer.


JavaScript runs on the client side (in the browser), and databases usually exist on the server side. You'll probably need to use an intermediate server-side language (like PHP, Java, .Net, or a server-side JavaScript stack like Node.js) to do the query.


Here's a tutorial on how to write some code that would bind PHP, JavaScript, and MySql together, with code running both in the browser, and on a server:


http://www.w3schools.com/php/php_ajax_database.asp


And here's the code from that page. It doesn't exactly match your scenario (it does a query, and doesn't store data in the DB), but it might help you start to understand the types of interactions you'll need in order to make this work.


In particular, pay attention to these bits of code from that article.


Bits of Javascript:


xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();

Bits of PHP code:


mysql_select_db("ajax_demo", $con);
$result = mysql_query($sql);
// ...
$row = mysql_fetch_array($result)
mysql_close($con);

Also, after you get a handle on how this sort of code works, I suggest you use the jQuery JavaScript library to do your AJAX calls. It is much cleaner and easier to deal with than the built-in AJAX support, and you won't have to write browser-specific code, as jQuery has cross-browser support built in. Here's the page for the jQuery AJAX API documentation.


The code from the article


HTML/Javascript code:


<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

PHP code:


<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

[#88703] Tuesday, December 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
viridianaw

Total Points: 154
Total Questions: 94
Total Answers: 89

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;