0

私は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">&lt&lt 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 &ltp&gt, &ltdiv&gt, &ltul&gt, &ltol&gt, &ltli&gt tags in your blog posts.</li>
    <li>Insert the <b>&ltjumpbreak&gt</b> tag where you would like content to stop displaying on the main blog page.</li>
    <li>Enclose all remaining content after the <b>&ltjumpbreak&gt</b> in a <b>&ltdiv class="hideAfterJumpBreak"&gt&lt/div&gt</b> tag pair. This will keep all content after the <b>&ltjumpbreak&gt</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); ?>
4

1 に答える 1

0

私のUPDATE投稿SETで、2番目の行の更新後にコンマがありました。おすすめの読み物もありがとうございます。

于 2013-01-04T17:32:24.253 に答える