这是我的HTML代码:
<div class="form-horizontal row-border">
<div class="form-group">
<label class="col-md-2 control-label">Title:</label>
<div class="col-md-10"><input class="form-control" id="title" name="title" type="text"></div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Article:</label>
<div class="col-md-10">
<textarea name="editor1" id="editor" rows="10" cols="80">
Let's go...
</textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Tags:</label>
<div class="col-md-10"><input class="tags" id="tags" type="text" value="">
</div>
</div>
<div class="row" style="margin-left:92%;">
<input type="button" id="PostArticle" class="btn btn-success" value="Post Article"></input>
</div>
</div>
这是我的JavaScript代码:
<script type="text/javascript">
$("#PostArticle").click(function() {
var title = $("#title").val();
var text = $("#editor").text();
var tags = $("#tags").val();
$.ajax({
url: 'post_new_article.php',
type: 'POST',
data: {
title: title,
tags: tags,
text: text
},
dataType: 'json',
success: function (data) {
noty({text: 'MySite.com:' + data.title + ' was successfully created!', type: 'success'});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
noty({text: 'Failure - The article was not created! Error is: ' + errorThrown + '', type: 'error'});
}
});
});
</script>
以下代码来自:post_new_article.php:
<?php
require "include/config.php";
require "include/functions.php";
ConnectWithMySQLDatabase();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$title = $_POST['title'];
$text = $_POST['text'];
$tags = $_POST['tags'];
$month = date('F');
$year = date('Y');
$day = date('d');
if(isset($_POST['title']))
{
mysql_query("INSERT INTO `Blog` (`id`, `Title`, `Article`, `Autor`, `Date`, `Month`,`Year`, `Tags`, `Image`) VALUES ('', '$title', '$text', 'Venelin Vasilev', '$day','$month', '$year', '$tags', '')");
}
$result['title'][0] = $title;
echo json_encode($result);
从所有事情来看,似乎只有MySQL插入功能不起作用。我可以确认ConnectWithMySQLDatabase();
函数正在按预期工作,此函数正在建立与MySQL的连接。
不知怎的,我点击Post Article
按钮后似乎无法插入mysql查询。我可以确认收到来自post_new_article.php
的回复,因为我收到一条通知,其中包含该文章的标题作为回复。所以json似乎回读了文章的标题。
那么你可以帮我解决这个问题并让它将查询插入MySQL数据库吗?
提前致谢!
答案 0 :(得分:0)
试试这个
表单中的 submit
按钮应为
<input type="submit" id="submit" class="btn btn-success" value="Post Article"></input>
AJAX应该是
<script>
$(function(){
$( "#submit" ).click(function(event)
{
event.preventDefault();
var title = $("#title").val();
var text = $("#editor").text();
var tags = $("#tags").val();
$.ajax(
{
type:"post",
dataType: 'json',
url: "./post_new_article.php",
data:{ title:title, text:text,tags:tags},
success:function(data)
{
}
});
});
});
</script>
编辑01
更改此
mysql_query("INSERT INTO Blog (id, Title, Article, Autor, Date, Month,Year, Tags, Image) VALUES ('', '$title', '$text', 'Venelin Vasilev', '$day','$month', '$year', '$tags', '')");