// create_update_post.php
try {
$stmt = executeQuery($query, $params);
// Return a success message or response
$response = ['success' => true];
echo json_encode($response);
} catch (PDOException $e) {
// Handle database errors
echo json_encode(['error' => 'Database error']);
}
// admin.js
$(document).ready(function () {
// creat update post form
$('#post-form').submit(function (e) {
e.preventDefault();
let postForm = $(this);
let csrf = postForm.data('csrf');
// Serialize form data
var formData = new FormData(this);
// Construct the dynamic URL to create_update_post.php
var baseUrl = window.location.protocol + '//' + window.location.host + '/';
var dynamicUrl = baseUrl + 'blog/includes/create_update_post.php';
$.ajax({
url: dynamicUrl, // Replace with the actual URL for creating/updating posts
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
// Handle the response, e.g., show a success message or redirect
console.log(response); // Log the response to the console
console.log(response.success); // Log the response to the console
if (response.success) {
// Refresh the page after a successful post
location.reload();
} else {
// Handle errors or show a message
console.error('Error:', response.error);
}
},
error: function (error) {
console.error(error);
}
});
});
});
سلام
وقت بخیر
در صفحه php کوئری با موفقیت اجرا میشه، دادههای فرم در دیتابیس وارد میشه و echo json_encode($response) اجرا میشه. در برگشت console.logهای داخل success: function (response) در js اجرا میشه و جواب به صورت
{"success":true}
undefined
نمایش داده میشه.
مشکل اینجاست که response.success مقدار undefined برمیگردونه در حالی که به نظر میاد باید True برگردونه و به همین دلیل شرط بعدی یعنی
if (response.success) {
// Refresh the page after a successful post
location.reload();
} else {
// Handle errors or show a message
console.error('Error:', response.error);
}
وارد else میشه. ممنون میشم راهنمایی بفرمایید دلیل اینکه response.success مقدار undefined برمیگردونه چی هست در حالی که ما {"success":true} رو داریم؟