我的Ajax请求返回成功,但脚本不是';t已执行


My Ajax request returns successful but the script isn't executed?

因此,在post_action.php中,我有一堆div被回显,以测试我是否在ajax请求期间访问了该文件。当我点击.deletePost时,我会收到一条警告,说"已成功删除",这让我相信ajax请求正在工作,但post_action.php中内容为"hello world"的div不存在。有什么想法吗?

post_action.php

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
if($_GET['action'] == "deletePost")
        deletePost($_GET['postTitle']);
function deletePost($title){
    $sql = "DELETE FROM blog WHERE Title = '$title'";
    mysqli_query($mysqli, $sql);
}
?>

functions.php

<?php
function sec_session_start() {
    $session_name = 'sec_session_id'; // Set a custom session name
    $secure = false; // Set to true if using https.
    $httponly = true; // This stops javascript being able to access the session id. 
    ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
    $cookieParams = session_get_cookie_params(); // Gets current cookies params.
    session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); 
    session_name($session_name); // Sets the session name to the one set above.
    session_start(); // Start the php session
    session_regenerate_id(); // regenerated the session, delete the old one.  
}
?>

dbconnect.php

<?php
$host="localhost"; // Host name
$username="root"; // username
$password="********"; // password
$dbname="nightowl"; // Database name
$tblname="blog"; // Table name
$mysqli=mysqli_connect($host,$username,$password,$dbname);
mysql_connect("$host", "$username", "$password");
mysql_select_db("$dbname");
?>

Javascript

$(document).ready(function(){
$('.deletePost').click(function(){
    $.ajax({
        url:"scripts/post_action.php",
        data: {action: "deletePost",  postTitle: $(this).siblings("h3.blog").text()},
        success: function(){
            alert('DELETED SUCCESSFULLY');
        }
    });
});
});

在HTML中,您必须首先准备一个元素(在本例中为div):

<div id='container'>
</div>

然后你的成功ajax:

success: function(response){ //retrieves the return from php
        $("#container").html(response); //put the return inside the element that has id = container
        alert('DELETED SUCCESSFULLY');
    }

所以,你的:

echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";

将显示在id为container的div内

要使php post_actions.php脚本中的div出现,您需要执行以下操作:

success: function(response){
        $("#containerWhereDivsCome").html(response);
        alert('DELETED SUCCESSFULLY');
    }