-1

I'm having trouble with making a deleting system in PHP,Javascript and AJAX. I've done it before but this time PHP and AJAX won't make contact, nor will the Javascript script do anything.

In the Javascript file(server-log.js) alerting something won't work, it will not come. This also won't work for the AJAX/PHP handler file(server-log.handler.php). The console bug doesn't give any bugs in return, the URLs in all the files are also correct. That's because I can find every file with the console bug from Chrome

The JS script(server-log.js):

    $('.btn btn-xs btn-danger').click(function(){

    var press = $.this.attr('id');
    var press_url = 'action=delete&id='+press;

        $.ajax({
            type:       'POST',
            url:        'panel/includes/handlers/server-log.handler.php',
            data:       press_url,
            success:    function(responseText)
            {
                $.parseJSON(responseText);

                    /* If and else controlling system  */
                    if(responseText.indexOf(1) > -1)
                    {
                        $("#danger").append('<strong>Helaas!</strong> Het is niet gelukt om de gevraagde log rij te verwijderen');
                    }
                    else if(responseText.indexOf(2) > -1)
                    {
                        $("#danger").append('<strong>Helaas!</strong> Het opgegeven ID nummer is niet geldig');
                    }
                    else if(reponseText.indexOf(100) > -1)
                    {
                        $("#success").append('<strong>Goed!</strong> De gevraagde log is succesvol verwijderd');
                    }
                    else
                    {
                        $("#info").append('<strong>Info:</strong> Er is helaas een onbekende fout opgetreden <i>2255200914</i>');
                    }
            }
        });
});

The HTML part(server-log.php)

                    <div class="alert alert-success" style="display: none;" id="success"></div>
                    <div class="alert alert-danger" style="display: none"; id="danger"></div>
                    <div class="alert alert-info" style="display: none;" id="info"></div>
                <div class="error-log sscroll">
                <ul>
                <form method="post">
                <?php
                while($arrayLogs = $arrayqLogs->fetch_array())
                {
                ?>
                    <li>
                    <span class="green">[<?php echo $arrayLogs['date']; ?>]</span> 
                    <span class="red">[<?php echo $arrayLogs['action']; ?>]</span> 
                    [<?php echo $arrayLogs['name']; ?>] <?php echo $arrayLogs['url']; ?> 
                    <button class="btn btn-xs btn-danger" name="deleting" id="<?php echo $arrayLogs['id']; ?>"><i class="fa fa-times"></i></button>
                    </li>
                <?php
                }
                ?>
                </form>
                </ul>
                </div>

The PHP handler part(server-log.handler.php)

    <?php
include_once '../config.php';

    if(isset($_POST['action']) && $_POST['action'] == 'delete')
    {
        $intAction      = trim($_POST['id']);
        $error          = array();
        $bolean         = false;

            # Prepared statement
            $stmt = $mysqli->prepare("SELECT id,name,action,url,date FROM logs WHERE id = ?");
            $stmt->bind_param('i', $intAction);
            $stmt->execute();
            $intStatement = $stmt->num_rows();
            $stmt->close;

                /* Controlling part */
                if($intStatement == 0)
                {
                    $error[]    = 1;
                    $bolean     = true;
                }
                elseif(!ctype_digit($intAction))
                {
                    $error[]    = 2;
                    $bolean     = true;
                }
                    // Deleting and notifying
                    if($bolean == false)
                    {
                        $stmt = $mysqli->prepare("DELETE FROM logs WHERE id = ?");
                        $stmt->bind_param('i', $intAction);
                        $stmt->execute();
                        $stmt->close();

                            # Returning error code
                            $error[]    = 100;
                    }

            /* Returning the value in a readable way */
            header('Content-Type: application/json');
            echo json_encode($error);
    }
?>

So this isn't the first time I'm making this on my website, but I haven't done it often either so I think I've done many things wrong. I'm thanking you guys in advance for helping, English isn't BTW my mother tongue so sorry for any grammar/misspelled words.

6
  • 1
    Did you find the point where console.log() stops responding? Commented Sep 20, 2014 at 21:29
  • 1
    Why are you performing a SELECT query, but never using the result that it returns? Commented Sep 20, 2014 at 21:40
  • @brasofilo If I put it before the click function I then get only 1 response out of the 3 so... Commented Sep 20, 2014 at 21:47
  • @Barmar I've done that to get a false or true response for the if controle Commented Sep 20, 2014 at 21:47
  • 1
    That will tell you if the prepare() was successful. It doesn't tell you anything about whether the query returned anything. Use $stmt->num_rows. Commented Sep 20, 2014 at 21:49

1 Answer 1

2

This line is incorrect:

var press = $.this.attr('id');

It should be either:

var press = this.id;

or:

var press = $(this).attr('id');

You also need to assign the result of $.parseJSON to a variable:

responseText = $.parseJSON(responseText);

Or you could specify dataType: 'json' in your $.ajax() call, and jQuery will automatically parse the response for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.