简体   繁体   中英

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

So in post_action.php I have a bunch of div's being echo'd to test if I am reaching that file during my ajax request. When I click on .deletePost I get an alert saying 'Deleted Successfully' which leads me to believe the ajax request is working, but the div with content "hello world" from post_action.php are not there. Any thoughts?

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');
        }
    });
});
});

For the divs echoed in the php post_actions.php script to appear you need to do:

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

inside the HTML you have to prepare an element first (in this case is div ) :

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

and then your success 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');
    }

So, your :

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

will be appear inside the div with id container .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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