我试图创建一个网页,使用ajax将javascript数组写入mySql表,以调用.php脚本。当我点击我的div(.button)时,我从jQuery.min库中得到了这个错误 - TypeError: Illegal constructor.
,从未见过它。知道什么是错的吗?没有提供一个小提琴,因为有一个.php文件,如果你想看到它的实际效果,将整个内容上传到here。谢谢你的帮助
HTML:
<!doctype HTML>
<head>
<meta charset="UTF-8">
</head>
<body>
tis just a blank page to get scripts and whatnot
<br>
<br>
<div class="button">CLICK</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
的javascript:
var strings = [];
for(x=0;x<45;x++) {
strings.push([1,2,3]);
}
$('.button').click(function() {
makeAjaxRequest();
});
function makeAjaxRequest() {
$.ajax({
url: 'write.php', //where I'm sending data
type: 'get', //data type post/get
data: {name: $(strings)}, //The data to be send in a query string format or JSON
success: function(response) { //A callback function to be executed is the request was succesful. A ‘response’ variable will be returned.
console.log(response);
}
});
}
PHP:
<?php
$servername = "localhost";
$username = "topdecka_wtAdmin";
$password = "OnlyWr1te";
$dbname = "topdecka_writeTest";
$data = "%".$_GET['name']."%"; //data passed from strings variable
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table
$sql = "CREATE TABLE strings (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
strings TEXT NOT NULL,
INSERT INTO strings (strings)
VALUES ('$data')
)";
// use exec() because no results are returned
$conn->exec($sql);
echo "Table string created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>