0

PHP return to jquery / ajax not working,

In my edit function error message is displayed yet success function executed and in my delete function nothing is displayed yet success is executed..

Have tried everything :( (well obviously not everything...)

Any ideas?

currentPage.EditItem = function(id) {
if (confirm('Are you sure you wish to edit?')) {
console.log("DetailPage :: edit");
var itemm = $("#itemm").val();
var amount = $("#amount").val();
var statuss = $("#statuss").val();
var Uid = localStorage.getItem("Uid");
console.log(statuss);
 if (itemm == "") {
    alert("Please enter item");
} else if (amount == "") {
    alert("Please enter amount");
} else if (statuss == "") {
    alert("Please enter status");
} else {
$.ajax({
type:'POST',
url:'http://www.mywebsite.com/edit.php',    
data:{'Uid':Uid,'itemm':itemm,'amount':amount,'statuss':statuss},

success: function(data) {
alert("Edit item success");
window.location.href = "new_up.html";   
},
error: function() {
alert("Edit user failure");
},
 });
window.location.href = "new_up.html";

};
};
};

PHP

<?php

// Check connection stuff

$itemm_id = $_POST['Uid'];  
$itemm_itemm = $_POST['itemm'];
$itemm_amount = $_POST['amount'];
$itemm_statuss = $_POST['statuss'];
print($itemm_statuss );
$qry = "xxxxxxxxxxx";

if (!mysqli_query($con,$qry))
{
die('Error: ' . mysqli_error($con));
}
echo "success";

mysqli_close($con);

?>

DELETE

currentPage.deleteItem = function(id) {
if (confirm('Are you sure you wish to delete?')) {
var Uid = localStorage.getItem("Uid");

$.ajax({
type:'POST',
url:'http://www.mywersite.com/delete.php',
data:{'Uid':Uid},
success: function(data){
if(data == "YES"){
alert("Item deleted!");
window.location.href = "new_up.html";
}
else{
alert("can't delete the row")
}
},

});
};
};

PHP

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$itemm_id = $_POST['Uid'];

$qry = "DELETE FROM balance1 WHERE id ='$itemm_id'";
if (!mysqli_query($con,$qry))
{
die('Error: ' . mysqli_error($con));
}
echo "YES";

mysqli_close($con);

?>
1
  • #1, use parametered prepare statements, #2, there is no telling what the problem is by this small code, filling in some data, this code works fine for me. Maybe you should try doing JSON posting instead? Commented Feb 16, 2015 at 23:22

2 Answers 2

1

You might want to improve the indentation of your code before posting. It's hard to read this way. Posting a clear question with clear example code will get you more responses.

I didn't check the whole code because of this, but I think this will help you to find the error:

The success function of your ajax call gets fired whenever it manages to send its data to the server and gets a textual reply. This means it is also fired when the server returns an SQL error: this error message is also a reply.

You might want to view the results in the console to see what is going on:

success: function(data) {
    console.log(data);
},

Or have the script check the response to see if the expected string 'success' was returned or something else (like an SQL error):

success: function(data) {
    if(data == 'success'){
        // OK!
    }else{
        // The server didn't say 'success' so something fishy is going on.
    }
},

Also: 'success' will always be echoed the way you've written your code now. You should place it somewhere it will only be triggered when it was actually ok:

if (mysqli_query($con,$qry)){
    echo "success";
}else{
    die('Error: ' . mysqli_error($con));
}
Sign up to request clarification or add additional context in comments.

1 Comment

You might want to note that when sending text/html (as he is in his code), it will almost always add white space after said text. so success would actually be success with trailing white spaces. The rest of your answer seems exactly on point!
0

You could do:

if (!mysqli_query($con,$qry)) {
    header("HTTP/1.0 404 Not Found");
    exit;
}

which should trigger the error: option in your jQuery call.

The reason why your code won't work is because die('Error: ' . mysqli_error($con)); to jQuery actually means "success": the server returned some text as response with a header 200 - jQuery is not going to parse that text to see if it contains the word "Error".

To get the picture

1 Comment

I would steer away from sending a 404 header (as that is page not found, yet the page exists, so that isn't what you want..), that may raise the error function, but it's bad practice. You're best to find your 4XX header here and use the correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.