...' /> ...' />
🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ Ebrahim Shahi
create_update_post.php
جامعه وردپرس (برنامه نویسی) ایجاد شده در ۲۶ شهریور ۱۴۰۲
// 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} رو داریم؟

سلام وقت بخیر

من تا جایی که متوجه شدم از سوالتون

در واقع response بصورت string گرفته میشه نه به شکل آبجکت برای تبدیلش میتونید از کد‌های پایین استفاده کنید

var responseText = '{"success":true}';
var jsonResponse = $.parseJSON(responseText); // یا از JSON.parse(responseText) هم می‌توانید استفاده کنید
// حالا می‌توانید به عنوان یک شی JSON از اطلاعات درون آن استفاده کنید
if (jsonResponse.success) {
    // اگر "success" در شی JSON برابر با true باشد، اینجا کد خود را اجرا کنید
    // برای مثال:
    console.log("عملیات موفقیت آمیز بود.");
} else {
    // در غیر این صورت، اینجا کد برای موارد دیگر را قرار دهید
    console.log("عملیات ناموفق بود.");
}
ابوالفضل محجوب ۲۶ شهریور ۱۴۰۲، ۱۷:۰۵