我使用jquery在db中保存我的数据,但我的代码问题是当我点击页面加载的提交按钮并返回主页面时也无法将数据保存在数据库中..我可以'弄清楚我的代码有什么问题..
这是我的代码:
<?php
session_start();
include_once '../../include/CsppoAdmin.php';
$csppoAdmin = new CsppoAdmin();
$msg = '';
if (isset($_REQUEST['submit'])){
extract($_REQUEST);
$register = $csppoAdmin->add_news_release($date_story,$writer,$headline,$source,$story,$keywords);
if ($register) {
$msg = '<p style="color:green">New lesson was successful!</p>';
} else {
$msg = '<p style="color:green">Failed. Lesson inputted already exits please try again.</p>';
}
}
?>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$("#submit").click(function(e) {
e.preventDefault();
var date_story = $("#date_story").val();
var writer = $("#writer").val();
var headline = $("#headline").val();
var source = $("#source").val();
var story = $("#story").val();
var keywords = $("#keywords").val();
var dataString = 'date_story='+date_story+'&writer='+writer+'&headline='+headline+'&source='+source+'&story='+story+'&keywords='+keywords;
if(date_story=='' || source=='' || story=='' || keywords=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "dataentry/add_nr.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
});
</script>
<form role="form" action="" method="post">
<div class="col-lg-6">
<div class="form-group">
<label>Date of story</label>
<input type="date" name="date_story" id="date_story" class="form-control" placeholder="Enter date">
</div>
<div class="form-group">
<label>Writer/Author</label>
<input type="text" name="writer" id="writer" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Headline</label>
<input type="text" name="headline" id="headline" class="form-control" placeholder="Enter headline">
</div>
<div class="form-group">
<label>Source</label>
<input type="text" name="source" id="source" class="form-control" placeholder="Enter source">
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
<label>Story</label>
<textarea name="story" id="story" class="form-control" rows="10"></textarea>
</div>
<div class="form-group">
<label>Keywords</label>
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="Enter keywords separated by comma(,)">
</div>
<button name="submit" id="submit"class="btn btn-primary">Submit</button>
</div>
</form>
这是我的php oop,数据将保存在db:
中public function add_news_release($date_story,$writer,$headline,$source,$story,$keywords){
$query = "SELECT `news_id` FROM `news_release` WHERE `headline` = ?";
if($stmt = $this->db->prepare($query)){
$stmt->bind_param('s', $headline);
$stmt->execute();
$stmt->bind_result($id);
while ($stmt->fetch()) {
printf("%s\n", $id);
}
if ($id == 0){
$stmt = $this->db->prepare("INSERT INTO `news_release` (`date_story`,`writer`,`headline`,`source`,`story`,`keywords`,`post_date`,`poi_id`)
VALUES (?,?,?,?,?,?,?,?)");
$post_date = date('Y-m-d');
$poi_id = $_SESSION['poi_id'];
$stmt->bind_param('sssssssi', $date_story,$writer,$headline,$source,$story,$keywords,$post_date,$poi_id);
$stmt->execute();
return true;
}else {return false;}
$stmt->close();
}
mysqli_close($db);
$this->db = null;
}
答案 0 :(得分:0)
我的问题的答案应该是这样的:
jQuery的:
<script src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
$("#submit").click(function() {
$.post( $("#nrForm").attr("action"), $("#nrForm :input").serializeArray(), function(save){ $("#result").html(save); } );
clearInput();
});
$("#nrForm").submit(function(){
return false;
});
function clearInput(){
$('input[type="text"], textarea').val('');
}
</script>
我的表格:
<form id="nrForm" action="dataentry/save_nr.php" method="post">
<div class="col-lg-6">
<div class="form-group">
<label>Date of story</label>
<input type="date" name="date_story" id="date_story" class="form-control" placeholder="Enter date">
</div>
<div class="form-group">
<label>Writer/Author</label>
<input type="text" name="writer" id="writer" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Headline</label>
<input type="text" name="headline" id="headline" class="form-control" placeholder="Enter headline">
</div>
<div class="form-group">
<label>Source</label>
<input type="text" name="source" id="source" class="form-control" placeholder="Enter source">
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
<label>Story</label>
<textarea name="story" id="story" class="form-control" rows="10"></textarea>
</div>
<div class="form-group">
<label>Keywords</label>
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="Enter keywords separated by comma(,)">
</div>
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
检查是否插入:
<?php
session_start();
include_once '../../include/CsppoAdmin.php';
$csppoAdmin = new CsppoAdmin();
extract($_REQUEST);
$register = $csppoAdmin->add_news_release($date_story,$writer,$headline,$source,$story,$keywords);
if ($register) {
echo '<p style="color:green">News release was successful!</p>';
} else {
echo '<p style="color:green">Failed. Inputted already exits please try again. </p>';
}
?>
然后将数据插入db:
public function add_news_release($date_story,$writer,$headline,$source,$story,$keywords){
$query = "SELECT `news_id` FROM `news_release` WHERE `headline` = ?";
if($stmt = $this->db->prepare($query)){
$stmt->bind_param('s', $headline);
$stmt->execute();
$stmt->bind_result($id);
while ($stmt->fetch()) {
printf("%s\n", $id);
}
if ($id == 0){
$stmt = $this->db->prepare("INSERT INTO `news_release` (`date_story`,`writer`,`headline`,`source`,`story`,`keywords`,`post_date`,`poi_id`)
VALUES (?,?,?,?,?,?,?,?)");
$post_date = date('Y-m-d');
$poi_id = $_SESSION['poi_id'];
$stmt->bind_param('sssssssi', $date_story,$writer,$headline,$source,$story,$keywords,$post_date,$poi_id);
$stmt->execute();
return true;
}else {return false;}
$stmt->close();
}
mysqli_close($db);
$this->db = null;
}