I'm really sorry to post this here, but I've tried many things but nothing is solving my current problem. The problem is that I've got an AJAX part that has a deleting_id, trough AJAX the deleting_id needs to get to a separate PHP script. And there it will be deleted with a prepared statement.
But I'm not getting anything back, not trough the console bug or error_reporting.
This is the AJAX part:
$('.btn.btn-xs.btn-danger').click(function(){
var delete_id = $(this).attr('id');
var delete_id_url = 'action=deleting&id='+delete_id;
$.ajax({
type: "POST",
url: "/panel/includes/handlers/404.handler.php",
data: delete_id_url,
succes: function responseText()
{
$.parseJSON(responseText);
if(responsetext.indexOf(1) > -1)
{
alert('Er is helaas geen overeenkomst in onze database gekomen.');
}
else if(responsetext.indexOf(2) > -1)
{
alert('Het opgegeven ID nummer is helaas geen geldig getal.');
}
else if(responsetext.IndexOf(100) > -1)
{
alert('De opgegeven rij is verwijderd.');
$(delete_id).hide();
}
else
{
alert('or nah');
}
}
});
});
The 404.handler.php part:
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')
{
$intDelete = trim($_POST['id']);
$errors = array();
$stmt = $mysqli->prepare("SELECT id FROM error WHERE id = ?");
$stmt->bind_param('i', $intDelete);
$stmt->execute();
$intSelect = $stmt->num_rows();
$stmt->close();
// Controle part.
if($intSelect == 0)
{
$errors[] = 1;
$bolean = true;
}
if(!ctype_digit($intDelete))
{
$errors[] = 2;
$bolean = true;
}
if($bolean == false)
{
$errors[] = 100;
$stmt = $mysqli->prepare("DELETE FROM error WHERE id = ?");
$stmt->bind_param('i', $intDelete);
$stmt->execute();
$stmt->close();
}
header('Content-Type: application/json');
echo json_encode($errors);
console.log($errors);
}
I'm really sorry if I'm not clear or anything, English isn't my mother tongue so I'm having trouble with it. Thank you for helping in advance!
use this
success: function responseText(response)
{
$.parseJSON(response);
if(response.indexOf(1) > -1)
{
alert('Er is helaas geen overeenkomst in onze database gekomen.');
}
else if(response.indexOf(2) > -1)
{
alert('Het opgegeven ID nummer is helaas geen geldig getal.');
}
else if(response.IndexOf(100) > -1)
{
alert('De opgegeven rij is verwijderd.');
$(delete_id).hide();
}
else
{
alert('or nah');
}
}
});
Also remove in your PHP file
change the firs line from
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')
to
if(isset($_POST['action']) && $_POST['action'] == 'deleting')
and remove this line
console.log($errors);
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting')
isset returns a boolean, so this never passes. You need:
if(isset($_POST['action']) && $_POST['action'] == 'deleting')
EDIT
Also, as noted in comments and the other answer, console.log
is javascript, not php, so remove that. Lastly, whilst unrelated to your problem why are you posting ajax to a file named 404.handler.php
? The name suggests this file has a different purpose.
Did you know that you misspelled the word "succes" in your AJAX success
function?
You also did not send teh responseText variable to your success function:
success: function(responseText){
When troubleshooting this sort of problem, I usually insert some feedback tests inside my code.
The first check just ensures that the AJAX code has reached the PHP handler. If I see Got to here 01, then I know I got at least that far.
Next, I check at each point where the code could fail -- inside if statements, after a database lookup, etc. This helps me narrow down exactly which part of the code is failing. I never find the problem immediately and usually have to add a few more tests, and then a few more, and then "A ha! A missing semi-colon!"
For Example:
echo 'Got to here 01 / ';
if(isset($_POST['action']) && isset($_POST['action']) == 'deleting'){
echo 'Got to here 02 / ';
$intDelete = trim($_POST['id']);
$errors = array();
$stmt = $mysqli->prepare("SELECT id FROM error WHERE id = ?");
$stmt->bind_param('i', $intDelete);
$stmt->execute();
$intSelect = $stmt->num_rows();
$stmt->close();
echo 'Got to here 03 / ';
// Controle part.
if($intSelect == 0)
{
echo 'Got to here 04 / ';
$errors[] = 1;
$bolean = true;
}
if(!ctype_digit($intDelete))
{
echo 'Got to here 05 / ';
$errors[] = 2;
$bolean = true;
}
if($bolean == false)
{
echo 'Got to here 06 / ';
$errors[] = 100;
$stmt = $mysqli->prepare("DELETE FROM error WHERE id = ?");
$stmt->bind_param('i', $intDelete);
$stmt->execute();
$stmt->close();
}
echo 'Got to here 07 / ';
header('Content-Type: application/json');
echo json_encode($errors);
console.log($errors);
}
javascript/jQuery:
$.ajax({
type: "POST",
url: "/panel/includes/handlers/404.handler.php",
data: delete_id_url,
success: function(responseText){
alert( responseText );
//the rest of your code follows
}
Now, you can see what you are getting back from the server, without guessing.