如何将PHP Post数据导入jquery ajax请求?

时间:2013-11-28 05:35:20

标签: javascript php jquery mysql ajax

我无法弄清楚如何将博客标题放入ajax调用的数据字段中。我一直在寻找关于初学SQL / PHP / AJAX的教程,我很挣扎,所以如果有人有这方面的任何好教程,我会很高兴听到它。我理解jquery和php没关系,但我正在尝试建立一个简单的博客系统,你可以使用ajax请求编辑和删除帖子,我正在努力工作。谢谢!

functions.js

$(document).ready(function(){
$('#deletePost').click(function(){
    $.ajax({
        url:"post_action.php",
        data: { action: "deletePost", postTitle: title of blog post },
        success: function(result){
            $('ul.left').html(result);
        }
    });
});
});

的index.php

<?php
            include 'scripts/db_connect.php';
            include 'scripts/functions.php';
            sec_session_start();
            $sql = "SELECT * FROM blog";
            $result = mysqli_query($mysqli, $sql);
            while($row = mysqli_fetch_array($result))
            {
                echo'<div class="blog"><h3 class="blog">' . $row['Title'] . 
                "</h3><h3>" . $row['Date'] . "</h3><h3>" . $row['Tag'] . 
                "</h3><hr>";
                echo'<p class="blog">' . $row['Body'] . '</p><form name="postForm" 
                method="post" action="process_post.php">
                <input type="radio" name="postAction" 
                value="editPost" class="postButton"  type="button">Edit</input>
                <input type="radio" name="postAction" value="deletePost" 
                class="postButton" type="button">Delete</input>
                <input type="radio" name="postAction" value="commentPost" 
                class="postButton"  type="button">Comment</input>
                </form></div>';
            }

            ?>

1 个答案:

答案 0 :(得分:1)

您不能在每篇博文中重复id="deletePost"。你应该使用一个类。然后你可以写:

$('.deletePost').click(function(){
    $.ajax({
        url:"post_action.php",
        data: { action: "deletePost",
                postTitle: $(this).siblings("h3.blog").text()
        },
        success: function(result){
            $('ul.left').html(result);
        }
    });
});