Droide
Droide

Reputation: 1859

Why do I obtain always error with ajax into my php web page?

I want delete a record from database and to do it I want use ajax..

So I have a table where I put into last td this:

<input type='image' src='./img/delete.png' onClick='deleteUser(".$utentiIscritti[$i][0].");' />

this is my deleteUser function:

function deleteUser(id){
                                    $.ajax({
                                        type:"post",
                                        url: "deleteUserAjax.php",
                                        data: {'id':id},
                                        success: function(data){
                                            console.log("OK");
                                            location.reload();
                                        },
                                       error: function(xhr, status, error){
                                           alert(xhr+"\n\n"+status+"\n\n"+error);
                                           console.log("KO");
                                       }
                                    });
                                }

And this is my php page to connect to db and delelte the record:

<?php
$USERDB = "u";
$PASSWORDDB = "p";
$NAMEDB = "d";

$queryDeleteUser = 'delete from user where id = "'.$_POST['id'].'"';
$conn = mysql_connect("localhost", $USERDB, $PASSWORDDB)
        or die("Errore nella connessione al database: " . mysql_error());
mysql_select_db($NAMEDB) or die("Errore nella selezione del database: " . mysql_error());
mysql_query($queryDeleteUser) or die("Errore nella query: " . $queryDeleteUser . "\n" . mysql_error());
dbDisconnect($conn);

But I obtain always (from every ajax request) error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)
iscritti.php:80

Why???

Upvotes: 1

Views: 49

Answers (2)

user4640949
user4640949

Reputation:

Maybe make it more cleaner:

HTML part:

<input type='image' src='./img/delete.png' value='<?=$id?>'>

jQuery part:

$(document).ready(function(){
    $("#delete").on("click", function(){
        var data = $(this).val();

        $.ajax({
            method: "POST",
            url: "page_you_handle_it.php?action=delete",
            data: {'id':id}
        }).done(function(data){
            //here you get response of your delete function!
        });
    });
});

PHP part:

$host = "[HOST]"; //Like localhost
$user = "[USER]"; //Like root
$pass = "[PASS]"; //Like 123
$db = "[DB]"; //Like users

$con = mysqli_connect($host, $user, $pass, $db) or die ("Conntecting the Database gone wrong");

$id = $_POST['id'];

$query_str = "DELETE FROM user WHERE id = '$id'";
$query = mysqli_query($con, $query_str);

if (!$query) //Do not run the `$query` in the return parts because it already runs when you say `if (!$query)`
{
    echo 'Delete gone wrong';
}
else
{
    echo 'Delete succes!';
}

Upvotes: 1

Ionic
Ionic

Reputation: 3935

You can consider two solutions.

  1. Your code is buggy. Try to execute it on it's own. Just call it in your browser and check the result!
  2. You have specified a relational path for your script. url: "deleteUserAjax.php", try instead an absolute path and check the result (url: "http://yourdomain.com/deleteUserAjax.php")

Upvotes: 1

Related Questions