简体   繁体   English

如何获取PHP Post数据到jquery ajax请求?

[英]How to get PHP Post data into jquery ajax request?

I can't figure out how to get the blog title into the data field of the ajax call. 我不知道如何使博客标题进入ajax调用的数据字段。 I have been looking for tutorials on beginner SQL / PHP / AJAX and I am struggling so if anyone has any good tutorials for this kind of stuff I'd be happy to hear it. 我一直在寻找有关SQL / PHP / AJAX初学者的教程,但我一直在努力,因此,如果有人对此类内容有任何好的教程,我将很高兴听到。 I understand jquery and php okay, but I am trying to set up a simple blog system where you can edit and delete posts using ajax requests and I am struggling big time. 我了解jquery和php可以,但是我正在尝试建立一个简单的博客系统,您可以在其中使用ajax请求编辑和删除帖子,而我正在努力工作。 Thanks! 谢谢!

functions.js 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 的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>';
            }

            ?>

You can't repeat id="deletePost" in each blog post. 您不能在每个博客文章中重复id="deletePost" You should use a class instead. 您应该改用一个类。 Then you can write: 然后您可以编写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM