从PHP脚本接收数据时出现问题。我正在通过AJAX向PHP发送表单数据,但值似乎为空,但在我的url中已设置了值。那么为什么我不用JavaScript重新获得我的价值观。
这是我的代码:
<!doctype html>
<html>
<head>
<title>Schachbrett</title>
<meta charset="utf-8">
<style>
table {
width: 800px;
height: 800px;
border: 1px solid black;
}
table tr:nth-child(even) td:nth-child(even), table tr:nth-child(odd) td:nth-child(odd) {
width: 100px;
height: 100px;
background-color: black;
}
</style>
<script>
window.onload = function() {
document.getElementById("button").onclick = pre_generate;
}
function pre_generate() {
request = new XMLHttpRequest();
var columns = document.getElementById("columns").value;
var rows = document.getElementById("rows").value;
var url= "chess2.php?columns="+columns+"&rows="+rows;
request.onreadystatechange = generate;
request.open("GET", url, true);
request.send(null);
}
function generate() {
if (request.readyState === 4 && request.status === 200) {
alert(request.responseText);
}
else {
}
//
/*for(var i=0; i<=8; i++) {
document.getElementsByTagName("table").innerHTML = "<tr id='"+i+"'></tr>";
for(var j=0; j<=8; j++) {
document.getElementsById(i).innerHTML = "<td></td>";
}
}*/
//document.getElementsByTagName("table").css("width", 100*columns+"px").css("height", 100*rows+"px");
}
</script>
</head>
<body>
<form action="" method="">
<input id="columns" type="text" name="columns" placeholder="Zeilen">
<input id="rows" type="text" name="rows" placeholder="Spalten">
<input id="button" type="submit" name="send" value="Generieren">
</form>
<table>
</table>
</body>
</html>
PHP:
<?php
if(isset($_GET['columns']) && isset($_GET['rows'])) {
$cols = $_GET['columns'];
$rows = $_GET['rows'];
echo $cols . "|" . $rows;
}
/*else {
alert("Bitte geben Sie die Anzahl der Spalten und Zeilen ein!");
}*/
?>
感谢您的帮助。我真的不知道这是怎么回事,因为我已经用这种风格上了另一堂课,而且效果很好。