I have a PHP page that has a button to Delete a customer. The button code uses $.ajax to call the page that "deletes" the customer and returns a JSON object on success or failure. The problem is the JSON response includes html from my two includes. Here is the code from the main page:
$("#btnDelete").click(function(){ if (confirm("Are you sure you want to delete this customer and ALL their records?")) { $.ajax({ type: "POST", url: "/includes/delete_customer.php", data: "cust_id=<?php echo $custno; ?>", dataType : "json", success: function(msg){ alert("Success: " + msg); }, error: function(XMLHttpRequest, textStatus, errorThrown){ alert("Failure: " + errorThrown); } }); top.location = "/pages/edit_customer_list.php"; } });
Here is the code from the called page:
<?php header("Content-type: application/json"); include_once($_SERVER["DOCUMENT_ROOT"]."/includes/main.php"); include_once($_SERVER["DOCUMENT_ROOT"]."/includes/dbconnect.php"); // Don't actually delete, just mark as inactive $query = "UPDATE customers SET active=FALSE WHERE id=".$_POST["cust_id"]; $result = mysql_query($query); if ($result) { $return['success'] = true; $return['error'] = false; $return['msg'] = "This customer has been successfully deleted."; } else { $return['success'] = false; $return['error'] = true; $return['msg'] = mysql_error(); } mysql_free_result($result); echo json_encode($return); ?>
I added the header code but that didn't help. The json response includes the code from the two include file and I can't figure out why or how to get rid of it.
Also, am I indicating success/failure the correct way? I wonder because I don't know how my calling program knows to process the error function. What is the correct way to indicate an error or failure?