<?php
$conn=mysql_connect("localhost","root","") or die("数据库服务器连接错误".mysql_error());
mysql_select_db("hanoi",$conn) or die("数据库访问错误".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
$n = "<script>document.write(name);</script>";
$s = "<script>document.write(s);</script>";
echo "$name $score";
mysql_query("insert into rank (name,socre) values ('$n','$s')",$conn);
?>
现在需要从JavaScript提取两个变量 存在mysql里面
但是 存储之后的结果是这样的:
怎么正确导入我需要的数据呢
php是没办法直接操作js的
可以用ajax将js变量发送到服务端再写入数据库
你没搞清楚前台和后台的关系,js是前台的代码,必须发送给服务器,由服务器的页面完成数据库操作
你的页面应该是这样的,然后由另一个页面xxx.php负责处理数据,这样应该会读出来了把?
<body>
<button onclick="req();">button</button>
</body>
<html>
<head>
<script type="text/javascript">
var name = "abc";
var s = "sss";
function req() {
window.location.href="xxx.php?name=" + name + "&score=" + s;
}
</script>
</head>
<body>
<button onclick="req();">button</button>
</body>
</html>
name,s没赋值啊
只能用ajax或post、get提交后服务器才能读取到数据并保存。
js是运行在客户机上的,而php是运行在服务器端的,不通过桥梁是无法直接读取滴。
<?php
$conn=mysql_connect("localhost","root","") or die("数据库服务器连接错误".mysql_error());
mysql_select_db("hanoi",$conn) or die("数据库访问错误".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
$n = $_GET['name'];
$s = $_GET['s'];
echo "$name $score";
mysql_query("insert into rank (name,socre) values ('$n','$s')",$conn);
?>
然后访问你的地址http://xxx.xxx/xxx.php?name=demo&s=ttt
随机改变demo和ttt,数据库的值就在变