Have the following jQuery AJAX call. It functions correctly and pulls off the PHP/MySQL operations, but returns the request.fail
function instead of the normal done/success. Works fine if you comment/remove the MySQL query.
Did a lot of searching and believe it is something to do with the dataType
, which I've tried as text
, html
, and ""
according to other posts. Tried using html
as the dataType
and just changing the echo "Success.";
for standard HTML/text output after the PHP code, but no luck. Any help would be appreciated.
This code is a small portion of a much larger project that has been isolated for the sake of the question. The $variable is just an example and is within scope (and not causing an issue).
Calling File:
<script type="text/javascript">
function ajaxCall() {
var request = $.ajax({
url: '<?php echo "ajax.php?variable=$value"; ?>',
type: "GET",
dataType: "html"
});
request.done(function(msg) {
alert(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
AJAX File:
<?php
$variable = $_GET['variable'];
$sign = mysqli_query(mysqli_connect('localhost', 'user', 'password', 'database'), "INSERT INTO `Table` (`Column`) VALUES ('$variable')");
echo "Success.";
?>
Like I said above, you need to work on error reporting for this all to work as you want it. I would start by building errors coming back from your PHP
, then building the result back into JSON
like this:
<?php
ini_set('display_errors', false);
$variable = $_GET['variable'];
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
$json_arr['response'] = FALSE;
$json_arr['message'] = "Connection failed: " . $conn->connect_error;
} else {
$sql = "INSERT INTO `tbl1` (`col1`) VALUES ('$variable')";
$result = $conn->query($sql);
if ($result) {
$json_arr['response'] = TRUE;
$json_arr['message'] = "success";
}else{
$json_arr['response'] = FALSE;
$json_arr['message'] = $conn->error;
}
}
$conn->close();
header('Content-Type: application/json');
echo json_encode($json_arr);
Once that is set, you can then use AJAX to obtain your results in the following manner:
function ajaxCall() {
var request = $.ajax({
url: '<?php echo "success.php?variable=$value"; ?>',
type: "GET",
dataType: "json"
});
request.done(function (result) {
if (result.response) {
alert(result.message);
// your success code
} else {
alert(result.message);
// your fail code
}
});
request.fail(function (jqXHR, textStatus, error) {
alert("FAIL");
console.log(textStatus);
console.log(error);
});
}
ajaxCall();
/* js */
function callFunction() {
$.ajax({
url: <url>
type: 'GET',
data: {},
dataType: 'json',
success: function(response) {
alert(response.msg)
},
error: function() {
alert('Error!');
}
});
return false;
}
/* php */
echo json_encode([
'msg' => 'Message'
]);
exit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.