我有这个 AJAX 函数,它应该将数据传递给 php 代码,然后应该将它输入到正在发生的数据库中的数据中,但 AJAX 确实没有按应有的方式工作。
<?php
session_start();
include_once("../engine/database.php");
if (isset($_POST["name"],$_POST["password"]) ) {
$uname = htmlspecialchars(mysql_real_escape_string($_POST["name"]) );
$passwd = htmlspecialchars(mysql_real_escape_string($_POST["password"]) );
$query = "SELECT * FROM users WHERE username ='".$uname."' AND password='".$passwd."' LIMIT 1";
$query= mysql_query($query);
if(mysql_num_rows($query) == 1) {
$_SESSION['admin'] = "Welcome to the Admin Panel {$_POST['name']} ";
$_SESSION['onOff'] = 'on';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
<script type="text/javascript" src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinymce.init({
selector: "textarea#postCont",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8" async defer></script>
<title>Admin Panel</title>
</head>
<body>
<div class="pure-g-r" style="letter-spacing:0em; padding-left:2em; padding-right:2em;">
<div class="pure-u-5-10">
<form class="pure-form pure-form-stacked" action='../engine/engine.php' method='post'>
<fieldset>
<legend><?php echo $_SESSION['admin']; ?></legend>
<div style="margin:0 auto;">
<input type='text' name='postTitle' class="pure-input-1" placeholder='Article Title'/>
</div>
<textarea class="pure-input-1" name='postDes' name='postDes' cols='102' placeholder='Article Summary'></textarea>
<textarea id="postCont" name='postCont' cols='60' rows='10'></textarea>
<br />
<button type="submit" name="blogsubmit" class="pure-button pure-input-1 pure-button-primary">Submit</button>
</fieldset>
</form>
</div>
<div class="pure-u-1-10">
</div>
<div class="pure-u-4-10">
<h2 style="margin:0 auto;">Posted articles</h2>
<dl>
<dt>How to create a Text editor</dt>
<dd><a href=""><small><a href="">Edit</a></small> | <small><a href=""> Delete</a></small></a>
<dt>How to create a Text editor</dt>
<dd><a href=""><small><a href="">Edit</a></small> | <small><a href="">Delete</a></small></a>
</dl>
</div>
</div>
<script type="text/javascript">
function submit() {
var xhr = new XMLHttpRequest();
try {
xhr = new new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert(" Please upgrade your broswer");
return false;
}
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText);
} else {
alert("dang!!!");
return false;
}
} else {
alert("dang!!!! 2 buahahaha");
return false;
}
var title = document.getElementById('postTitle').value;
var postDes = document.getElementById('postDes').value;
var postCont = document.getElementById('postCont').value;
var data = "title=" + title + "&summary=" + postDes + "&content=" + postCont;
xhr.open("POST", "../engine/engine.php",true );
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(data);
}
}
</script>
</body>
</html>
<?php }
else {
echo "username or password is incorrect";
die(mysql_error());
}
}
?>
它应该将其传递给此代码
<?php
include_once('database.php');
if (isset($_POST['blogsubmit']) ) {
$title = mysql_real_escape_string($_POST['postTitle']);
$summary = mysql_real_escape_string($_POST['postDes']);
$content = mysql_real_escape_string($_POST['postCont']);
insertBlogPost($title,$summary,$content);
}
function insertBlogPost($title,$summary,$content) {
$query = "INSERT INTO `blogposts` SET
`title` = '$title',
`summary` = '$summary',
`content` = '$content' ";
mysql_query($query) or die(mysql_error());
echo "Yaaaay";
}
?>
问题是我得到一个打印 Yaaay 部分而不是警报的页面。我已经尝试了我所知道的任何方法来解决问题,所以我非常感谢您的帮助。我也尝试在 POST 的真假之间切换,但没有帮助。
PS:很抱歉使用 mysql_* ,我仍然不习惯使用 PDO。提前谢谢了。