将数据从javascript发送到mysql数据库
我有一个小小的点击计数器。我希望将每次单击都包含在mysql表中。有人能帮忙吗将数据从javascript发送到mysql数据库,javascript,mysql,Javascript,Mysql,我有一个小小的点击计数器。我希望将每次单击都包含在mysql表中。有人能帮忙吗 var count1 = 0; function countClicks1() { count1 = count1 + 1; document.getElementById("p1").innerHTML = count1; } document.write('<p>'); document.write('<a href="javascript:countClicks1();">Count
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>');
var count1=0;
函数countClicks1(){
count1=count1+1;
document.getElementById(“p1”).innerHTML=count1;
}
写(“”);
文件。写(“”);
文件。写(“”);
文档。写入(“0
”);
万一有人想看看我做了什么:
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>');
var count1=0;
函数countClicks1(){
count1=count1+1;
document.getElementById(“p1”).innerHTML=count1;
}
函数doAjax()
$.ajax({
类型:“POST”,
url:“phpfile.php”,
数据:“名称=名称&位置=位置”,
成功:功能(msg){
警报(“保存的数据:“+msg”);
}
});
}
文件。写(“”);
文件。写(“”);
文件。写(“”);
文档。写入(“0
”);
这是phpfile.php,出于测试目的,它将数据写入txt文件
<?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);
?>
您必须以某种方式将此数据提交到服务器。我假设您不希望每次用户单击链接时都重新加载整个页面,因此您必须使用XHR(AJAX)。如果您没有使用jQuery(或其他一些JS库),您可以阅读如何“手动”执行XHR请求。问题中定义的JavaScript不能直接使用MySql。这是因为它不是在同一台计算机上运行的
JavaScript在客户端(浏览器中)运行,数据库通常存在于服务器端。您可能需要使用中间服务器端语言(如PHP、Java、.Net或服务器端JavaScript堆栈,如Node.js)来执行查询
下面是一个教程,介绍如何编写一些将PHP、JavaScript和MySql绑定在一起的代码,同时在浏览器和服务器上运行代码:
这是那页的代码。它与您的场景并不完全匹配(它执行查询,并且不在数据库中存储数据),但它可能会帮助您开始了解为了使这项工作正常进行所需的交互类型
特别是,请注意那篇文章中的这些代码
Javascript的一些方面:
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
PHP代码位:
mysql_select_db("ajax_demo", $con);
$result = mysql_query($sql);
// ...
$row = mysql_fetch_array($result)
mysql_close($con);
另外,在你掌握了这类代码的工作原理之后,我建议你。它比内置的AJAX支持更干净、更容易处理,而且您不必编写特定于浏览器的代码,因为jQuery内置了跨浏览器支持。这是给你的那一页
文章中的代码
HTML/Javascript代码:
<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>
函数showUser(str)
{
如果(str==“”)
{
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getuser.php?q=“+str,true”);
xmlhttp.send();
}
选择一个人:
彼得·格里芬
路易斯·格里芬
格伦泥潭
约瑟夫·斯旺森
此处将列出人员信息。
PHP代码:
mysql_select_db("ajax_demo", $con);
$result = mysql_query($sql);
// ...
$row = mysql_fetch_array($result)
mysql_close($con);
<?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);
?>
其他海报是正确的,您无法直接从javascript连接到MySQL。
这是因为JavaScript在客户端,mysql在服务器端
因此,您最好使用ajax调用上面引用的处理程序,如果您可以让我们知道您的项目使用的是什么语言,我们可以更好地帮助您ie php/java/.net
如果您的项目使用的是php,那么Merlyn的示例是一个很好的起点,我个人会使用jquery.ajax()来减少您的代码,并有更好的机会减少跨浏览器问题
@victor-编辑答案的人:我复制/粘贴了w3schools文章中的代码。当您对代码进行编辑时,它不再是相同的代码,因此说“文章中的代码”毫无意义。我建议你在编辑之前编辑我引用的源代码,或者添加你自己的答案,或者更改该部分的标题,这样它就不再说它来自文章。很抱歉打扰你,但我只是想做得更好。您的答案有多张赞成票,被选为可接受答案。这就是为什么我认为推广不良做法(如肮脏的格式或SQL注入和FPD漏洞)是个坏主意。顺便说一句,我在W3Schools上检查过:他们已经格式化了代码并修复了SQL注入漏洞(顺便说一句,FPD仍然没有修复)。@Victor不麻烦,我只是觉得结果不太正确。我认为你的代码编辑(可能)很好。我没有完全检查它们。我只是觉得你也应该改变这个部分,这样你显然已经改进了它,而不仅仅是复制/粘贴了它。与“文章中的代码”类似,将其重命名为“代码示例(改编自上面链接的文章)”。或者你可以更新它,使之与源文章相匹配(在我看来,它似乎没有改变,只是简单地看了一眼,但现在我看到了他们所做的改变)。这两件事中的一件,如果你想继续做修改的话。我认为这是最小的问题,因为真正的问题是初学者会复制粘贴你的代码,认为它是好的。无论如何,谢谢你的解释。