我正在尝试使用 ajax 在我的数据库中插入数据,但没有任何效果。谁能告诉我我做错了什么?
索引.PHP
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function myLoad(){
if(window.XMLHttpRequest){
var xmlhttp = new XMLHttpRequest();
}else{
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('GET', 'generate.php?text=' + document.getElementById('textarea').value, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
window.document.getElementById('par').innerHTML = xmlhttp.responseText;
}
}
}
</script>
</head>
<body>
<div id="par"></div>
<input type="text" id="textarea">
<input type="button" id="button" value="Click Me" onClick="myLoad();"/>
</body>
</html>
生成.PHP
<?php
require 'php/connect.inc.php';
if(isset($_GET['text']) && !empty($_GET['text'])){
$name = $_GET['text'];
$query = "INSERT INTO users VALUES('', '$name', 'pass123', 'greg')";
if($mysql_query = mysql_query($query)){
echo 'Success';
}else{
echo 'Failed';
}
}
?>
CONNECT.INC.PHP
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_error = 'Could not connect';
$a_database = 'a_database';
if(!@mysql_connect($mysql_host, $mysql_user, $mysql_pass) OR !@mysql_select_db($a_database)){
die($mysql_error);
}
?>