我对 php 和 jQuery 很陌生。我已经在网上搜索了高和低的答案,但无济于事。我正在尝试“发布”一个使用 ajax 和 php 提交给自身的编辑表单。我能够将值从 ajax.php 返回到我的表单。我也可以在它们发布后看到它们,但是 UPDATE 查询似乎不起作用。除了从选择中获取值之外,我没有将 ajax 用于其他任何事情。某处毛茸茸的,我不知道在哪里。我也不太确定我的代码,但我所拥有的似乎可以达到/围绕更新查询。任何帮助深表感谢。
这是我的 html,ajax 调用在底部:
<?php //require_once("_includes/session.php"); ?>
<?php require_once("_includes/connection.php"); ?>
<?php require_once("_includes/functions.php"); ?>
<?php //confirm_logged_in(); ?>
<?php include("_includes/header.php"); ?>
<?php
if(isset($_POST['submit'])) {
//$id = $_POST['postid'];
//$title = $_POST['title'];
//$content = $_POST['content'];
//printf($id);
//printf($title);
//printf($content);
$errors = array();
// Form Validation
$required_fields = array('title', 'content');
foreach($required_fields as $fieldname) {
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('title' => 50);
foreach($fields_with_lengths as $fieldname => $maxlength ) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; }
}
if (empty($errors)) {
// Perform Update
$id = mysql_prep($_POST['postid']);
$title = mysql_prep($_POST['title']);
$content = mysql_prep($_POST['content']);
$query = "UPDATE posts SET
title = '{$title}',
content = '{$content}',
WHERE id = {$id}";
$result = mysqli_query($connection, $query);
if (mysqli_affected_rows($connection) == 1) {
// Success
$message = "The post was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br>" . mysqli_error($connection);
}
} else {
// Errors occurred
if (count($errors) == 1) {
$message = "There was " . count($errors) . " error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
} // End: if(isset($_POST['submit']))
?>
<section id="manageContent">
<a href="manage.php"><< Back</a>
<h2>Edit Blog Post</h2>
<form id="newPost" name="newpost" action="edit_post.php" method="post">
<label for="posttitle">Select post title to edit:</label>
<select id="titleName" name="titleName">
<?php
$post_set = get_all_posts();
while ($post = mysqli_fetch_array($post_set)) {
echo '<option value="' . htmlspecialchars($post['id']) . '">'
. htmlspecialchars($post['title'])
. "</option>";
}
?>
</select>
<p><label for="id">id:</label> <input id="postid" name="postid" type="text"></p>
<p><label for="title">Title:</label> <input id="title" name="title" type="text" required> (max length: 50 characters)</p>
<p><b>IMPORTANT!! things to remember so content is displayed how you would like:</b></p>
<ul>
<li>You can use <p>, <div>, <ul>, <ol>, <li> tags in your blog posts.</li>
<li>Insert the <b><jumpbreak></b> tag where you would like content to stop displaying on the main blog page.</li>
<li>Enclose all remaining content after the <b><jumpbreak></b> in a <b><div class="hideAfterJumpBreak"></div></b> tag pair. This will keep all content after the <b><jumpbreak></b> from displaying on the main blog page until the topic is displayed on the topic.php page.</li>
<li>Double check that every opening tag has a closing tag.</li>
</ul>
<p><label for="content">Content:</label></p>
<p><textarea id="content" name="content"></textarea></p>
<!--<p><button type="submit" name="submit">Edit Post</button></p> -->
<input id="submit" type="submit" name="submit" value="Edit Post">
<a href="edit_post.php"><button name="cancel">Cancel</button></a>
<a href="delete_post.php"><button name="delete">Delete</button></a>
</form>
<script>
(function() {
$("select#titleName").prop("selectedIndex", -1);
// returns a single item from php
/*
$("select#title").on("change", function() {
var post_id = this.value;
console.log(this);
$.ajax ({
url: "_includes/ajax.php",
type: "POST",
data: {post_id : post_id},
dataType: "text",
success: function(response) {
$( "input#title" ).val(response);
},
failure: function() {
alert("fail");
}
});
});
*/
$("select#titleName").on("change", function() {
var post_id = this.value;
console.log(this);
$.ajax ({
url: "_includes/ajax.php",
type: "POST",
data: {post_id : post_id},
dataType: "json",
success: function(response) {
$( "input#postid" ).val(response.post_id);
$( "input#title" ).val(response.post_title);
$( "textarea#content" ).val(response.post_content);
},
failure: function() {
alert("fail");
}
});
});
})();
</script>
</section>
<?php include("_includes/footer.php"); ?>
这是我的ajax.php:
<?php require_once("connection.php"); ?>
<?php require_once("functions.php"); ?>
<?php include_once("form_functions.php"); ?>
<?php
//if(isset($_POST['title'])) {
//$post_id = $_POST['post_id'];
//$query = "SELECT title, content
// FROM posts
// WHERE id = '$post_id'";
//$result = mysqli_query($connection, $query);
//$row = mysqli_fetch_array($result);
//echo $row['title'], $row['content'];
//}
$post_id = $_POST['post_id'];
$query = "SELECT *
FROM posts
WHERE id = '$post_id'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
echo json_encode(
array("post_id" => $row['id'],
"post_title" => $row['title'],
"post_content" => $row['content'])
)
//echo $row['title'], $row['content'];
?>
<?php mysqli_close($connection); ?>