Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601806
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:40:02+00:00 2026-05-18T20:40:02+00:00

I have a rather confusing problem. I have a php file (http://example.com/delete.php) <?php session_start();

  • 0

I have a rather confusing problem.

I have a php file (http://example.com/delete.php)

<?php
session_start(); 
$user_id = $_SESSION['user_id'];
$logged_in_user = $_SESSION['username'];
require_once('../classes/config.php');
require_once('../classes/post.php');
$post = new Post(NULL,$_POST['short']);
#print_r($post);
try {
 if ($post->user_id == $user_id) {
  $pdo = new PDOConfig();     

   $sql = "DELETE FROM posts WHERE id=:id";

   $q = $pdo->prepare($sql);
   $q->execute(array(':id'=>$post->id));       

  $pdo = NULL;
 }
 else {throw new Exception('false');}
}       
catch (Exception $e) {
   echo 'false';
}
?>

and I’m trying to get this jquery to post data to it, and thus delete the data.

$('.post_delete').bind('click', function(event) {

    var num = $(this).data('short');

    var conf = confirm("Delete This post? (" + num + ")");

    if (conf == true) {
        var invalid = false;
        $.post("http://example.com/delete.php", {short: num},
        function(data){

            if (data == 'false') {
                alert('Deleting Failed!');
                invalid = true;
            }
        });

        if (invalid == false) { 
          alert("post Has Been Deleted!");
        } 
        else {
            event.preventDefault();
            return false;
        }


    } 
    else {
        event.preventDefault();
        return false;
    }



});

and when I do that, it returns “Post Has Been Deleted!” but does not delete the post.

Confused by that, I made a form to test the php.

<form action="http://example.com/delete.php" method="POST">
<input type="hidden" value="8" name="short"/>
<input type="submit" name="submit" value="submit"/>
</form>

which works beautifully. Very odd.

I have code almost identical for deleting of a comment, and that works great in the javascript.

Any ideas? Beats me.

Thanks in advance,
Will

EDIT:
this works… but doesn’t follow the href at the end, which is the desired effect. Odd.

$('.post_delete').bind('click', function(event) {

    var num = $(this).data('short');

    var conf = confirm("Delete This Post? (http://lala.in/" + num + ")");

    if (conf == true) {
        var invalid = false;
        $.post("http://example.com/delete/post.php", {short: num},
        function(data){

            if (data == 'false') {
                alert('Deleting Failed!');
                invalid = true;
            }
        });

        if (invalid == false) { 
          alert("Post Has Been Deleted!");
        ******************************************
            event.preventDefault();
            return false;
        ******************************************
        } 
        else {
            event.preventDefault();
            return false;
        }


    } 
    else {
        event.preventDefault();
        return false;
    }



   });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:40:03+00:00Added an answer on May 18, 2026 at 8:40 pm

    If your PHP script delete the post, it doesn’t return anything.


    My bad, it’s not answering the real question, but still is a mistake 😉

    Actually, it seems that PHP session and AJAX doesn’t quite work well together sometimes.

    It means that if ($post->user_id == $user_id) will never validate, hence the non-deleting problem.

    2 ways to see this :

    1. Log $user_id and see if it’s not null
    2. Try to send the $_SESSION['user_id'] with your ajax post and check with it. But not in production, for security reason.

    1-

    Your PHP should return something in every case (at least, when you’re looking for a bug like your actual case).

    <?php
    [...]
    try {
     if ($post->user_id == $user_id) {
      [...]
      echo 'true';
     }
     else {throw new Exception('false');}
    }       
    catch (Exception $e) {
       echo 'false';
    }
    ?>
    

    2-

    jQuery is nice to use for AJAX for many reasons. For example, it handles many browsers and make checks for you but moreover, you can handle success and error in the same .ajax() / .post() / .get() function \o/

    $('.post_delete').bind('click', function(event) {
        var num = $(this).data('short'); // If that's where your data is... Fair enough.
        if (confirm("Delete This Post? (http://lala.in/" + num + ")")) {
            $.post("delete/post.php", {short: num}, // Relative is nice :D
            function(data){
                if (data == 'false') {
                    alert('Deleting Failed!');
                 }else{
                    alert("Post Has Been Deleted!");
                    // Your redirection here ?
                }
            });
         }
    });
    

    3-

    If you need to send data from a form to a script and then do a redirection, I won’t recommand AJAX which is usually use not to leave the page !
    Therefore, you should do what’s in your comment, a form to a PHP script that will apparently delete something and then do a redirection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a rather confusing problem. Web Service A - Called directly by Win32
I have a function that I use on index.php page and I would like
I'm currently working on my own PHP Framework, and I need some help figuring
I'm new to PHP so I apologize if this seems silly. I've searched around
Clicking on an element which has a Javascript handler makes the element go have
I have two divs (one inside of the other) and am running into a
I am developing a script to read and search log files for a big
This seems to be a bit of an infamous error all over the web.
As part of upgrading JRun, we are moving from a 1.4 JVM to a
I've been on and off with the C language for the past year(?) until

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.