javascript - 通过 php 创建 Javascript 数组
问题描述
在这上面花费了令人尴尬的几个小时之后,我仍然无法找到答案。我有一个 Javascript/HTML 程序,其中有一个大型动态表格,表格的单元格中有很多按钮。当用户完成页面时,我想将用户对按钮的选择发送到数据库。然后,我将使用接收 php 程序中的那些来显示图表。我已经将按钮的所有值放入一个 Javascript 数组中,所以现在我想发送整个数组,然后在新程序中将它们用作 Javascript 数组。每次我尝试这个,我都没有得到任何结果。这是一个基于我在 stackoverflow 上找到的示例:发送程序:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<script>
var ourObj={};
ourObj.data="4";
ourObj.arPoints = [{'x':1, 'y': 2},{'x': 2.3, 'y': 3.3},{'x': -1, 'y': -4}];
$.ajax({
url: 'testb2.php',
type: 'post',
data: {"points" : JSON.stringify(ourObj)},
success: function(data) {
alert("success");
}
});
</script>
</html>
接收php程序:
<?php>
// Test if our data came through
echo "starting";
if (isset($_POST["points"])) {
// Decode our JSON into PHP objects we can use
$points = json_decode($_POST["points"]);
// Access our object's data and array values.
echo "Data is: " . $points->data . "<br>";
echo "Point 1: " . $points->arPoints[0]->x . ", " . $points->arPoints[0]->y;
$servername = "localhost";
$username = "xxx";
$password = "yyy";
$dbname = "zzz";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "
INSERT INTO FootprintAction(UserID, ActionValue, fpID)
VALUES (?, ?, ?);
";
if ($stmt = mysqli_prepare($conn, $sql)) {
mysqli_stmt_bind_param($stmt, "iss", $points->data, "1", "1");
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
} else {
//echo "no result from attempt to insert people into fpAction". "<br>";
//echo $sql;
}
}
?>
我得到的是来自发送程序的一个空白屏幕,只有一个警告说“成功”(即,接收程序没有回答)并且没有插入数据库。
你能建议我做错了什么吗?或者更好的是,有没有一种简单的方法使用 php 将 javascript 数组从客户端程序获取到服务器端程序?我要发送的数组是一组字符串数据,没有标签;这是数组的一行:
actions[ 5] = new Array( "Install Photovoltaic (Solar) Panels","6.33","1020","33121","0","25","149.8","0","0","0","1","1","Photovoltaic (solar) systems allow you to produce green electricity on your own rooftop. Vendors often offer both purchase and lease options. ","3","1.5","10","5.75","0");
我想在接收程序中得到一个与发送程序中相同的 Javascript 数组。谢谢!!!
解决方案
推荐阅读
- python - 无法弹出网页(python、selenium)
- git - Git 日志多行输出
- ubuntu - 为函数添加了函数参数
- python - 使用 nf 过滤器在 mininet 中的主机充当 mininet SDN 模拟中的代理
- toit - 如何重置 PubSub 中未确认的消息?
- google-cloud-platform - 谷歌云中有没有办法阻止访问特定网址的尝试
- python - sort_values() 与 'key' 对数据框中的一列元组进行排序
- pytorch - 在 pytorch 强化学习中更改输入类型的问题
- linux - 如何独立于扬声器控制键盘上的静音 LED
- ios - 如何使用 iOS 共享扩展将 pdf 文件从电子邮件/iOS 设备共享到 iOS 应用程序?