upload images on server side and save url in mysql database

for my codeigniter 3 view page i have a form submission to get new data to sql tale in phpmyadmin:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div class="container mt-5">
<button class="btn btn-primary" data-toggle="modal" data-target="#directorModal">Add Director</button>
<!-- Modal -->
<div class="modal fade" id="directorModal" tabindex="-1" aria-labelledby="directorModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="directorModalLabel">Add Director</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form id="directorForm" method="get" enctype="multipart/form-data" action='<?=$user_base_url?>/profil/save_directors'>
<div class="form-group">
<label for="director_name">Director Name</label>
<input type="text" class="form-control" name="director_name" required>
</div>
<div class="form-group">
<label for="info">Info</label>
<textarea class="form-control" name="info" required></textarea>
</div>
<div class="form-group">
<label for="position">Position</label>
<input type="text" class="form-control" name="position" required>
</div>
<div class="form-group">
<label for="director_image">Director Image</label>
<input type="file" class="form-control" name="director_image" required>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
</div>
</div>
</code>
<code><div class="container mt-5"> <button class="btn btn-primary" data-toggle="modal" data-target="#directorModal">Add Director</button> <!-- Modal --> <div class="modal fade" id="directorModal" tabindex="-1" aria-labelledby="directorModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="directorModalLabel">Add Director</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span>×</span> </button> </div> <div class="modal-body"> <form id="directorForm" method="get" enctype="multipart/form-data" action='<?=$user_base_url?>/profil/save_directors'> <div class="form-group"> <label for="director_name">Director Name</label> <input type="text" class="form-control" name="director_name" required> </div> <div class="form-group"> <label for="info">Info</label> <textarea class="form-control" name="info" required></textarea> </div> <div class="form-group"> <label for="position">Position</label> <input type="text" class="form-control" name="position" required> </div> <div class="form-group"> <label for="director_image">Director Image</label> <input type="file" class="form-control" name="director_image" required> </div> <button type="submit" class="btn btn-primary">Save</button> </form> </div> </div> </div> </div> </div> </code>
<div class="container mt-5">
    <button class="btn btn-primary" data-toggle="modal" data-target="#directorModal">Add Director</button>

    <!-- Modal -->
    <div class="modal fade" id="directorModal" tabindex="-1" aria-labelledby="directorModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="directorModalLabel">Add Director</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span>×</span>
                    </button>
                </div>
                <div class="modal-body">
                <form id="directorForm" method="get" enctype="multipart/form-data" action='<?=$user_base_url?>/profil/save_directors'>
                    <div class="form-group">
                        <label for="director_name">Director Name</label>
                        <input type="text" class="form-control" name="director_name" required>
                    </div>
                    <div class="form-group">
                        <label for="info">Info</label>
                        <textarea class="form-control" name="info" required></textarea>
                    </div>
                    <div class="form-group">
                        <label for="position">Position</label>
                        <input type="text" class="form-control" name="position" required>
                    </div>
                    <div class="form-group">
                        <label for="director_image">Director Image</label>
                        <input type="file" class="form-control" name="director_image" required>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
                </div>
            </div>
        </div>
    </div>
</div>

here i try to get new user info to save in sql table as well their images in server.

in controller i have following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public function save_directors() {
$this->load->library('upload');
$config['upload_path'] = './uploads/board_of_directors_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg'; // Adjust as needed
$config['max_size'] = 2048; // 2MB max size
$config['encrypt_name'] = TRUE; // Encrypt the file name
$this->upload->initialize($config);
if ($this->upload->do_upload('director_image')) {
$upload_data = $this->upload->data();
$director_image = $upload_data['file_name'];
} else {
$error = $this->upload->display_errors();
log_message('error', 'Image upload error: ' . $error);
$director_image = NULL; // Set to NULL or handle accordingly
}
$director_data = [
'director_name' => $this->input->get('director_name'),
'info' => $this->input->get('info'),
'position' => $this->input->get('position'),
'director_image' => $director_image
];
$this->db->insert('board_of_directors', $director_data);
if ($this->db->affected_rows() > 0) {
echo json_encode(['status' => 'success']);
} else {
echo json_encode(['status' => 'error']);
}
}
</code>
<code>public function save_directors() { $this->load->library('upload'); $config['upload_path'] = './uploads/board_of_directors_images/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; // Adjust as needed $config['max_size'] = 2048; // 2MB max size $config['encrypt_name'] = TRUE; // Encrypt the file name $this->upload->initialize($config); if ($this->upload->do_upload('director_image')) { $upload_data = $this->upload->data(); $director_image = $upload_data['file_name']; } else { $error = $this->upload->display_errors(); log_message('error', 'Image upload error: ' . $error); $director_image = NULL; // Set to NULL or handle accordingly } $director_data = [ 'director_name' => $this->input->get('director_name'), 'info' => $this->input->get('info'), 'position' => $this->input->get('position'), 'director_image' => $director_image ]; $this->db->insert('board_of_directors', $director_data); if ($this->db->affected_rows() > 0) { echo json_encode(['status' => 'success']); } else { echo json_encode(['status' => 'error']); } } </code>
public function save_directors() {
    $this->load->library('upload');
    $config['upload_path'] = './uploads/board_of_directors_images/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; // Adjust as needed
    $config['max_size'] = 2048; // 2MB max size
    $config['encrypt_name'] = TRUE; // Encrypt the file name
    $this->upload->initialize($config);
    if ($this->upload->do_upload('director_image')) {
        $upload_data = $this->upload->data();
        $director_image = $upload_data['file_name'];
    } else {
        $error = $this->upload->display_errors();
        log_message('error', 'Image upload error: ' . $error);
        $director_image = NULL; // Set to NULL or handle accordingly
    }
    $director_data = [
        'director_name' => $this->input->get('director_name'),
        'info' => $this->input->get('info'),
        'position' => $this->input->get('position'),
        'director_image' => $director_image
    ];
    $this->db->insert('board_of_directors', $director_data);
    if ($this->db->affected_rows() > 0) {
        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'error']);
    }
}

and tha ajax in view page:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(document).ready(function() {
$('#directorsForm').on('submit', function(event) {
event.preventDefault(); // Prevent the form from submitting the traditional way
// Create a FormData object to hold the form data, including the file
var formData = new FormData(this);
$.ajax({
url: '<?=$user_base_url?>/profil/save_directors', // Adjust the URL according to your setup
type: 'GET',
data: formData,
processData: false, // Important! Prevent jQuery from automatically transforming the data into a query string
contentType: false, // Important! Prevent jQuery from overriding the content type
dataType: 'json',
success: function(response) {
if (response.status === 'success') {
alert('Director saved successfully!');
// Optionally, redirect or refresh the page
} else {
alert('Failed to save director.');
}
},
error: function(xhr, status, error) {
console.log('AJAX error:', error);
alert('An error occurred while saving the director.');
}
});
});
});
</code>
<code>$(document).ready(function() { $('#directorsForm').on('submit', function(event) { event.preventDefault(); // Prevent the form from submitting the traditional way // Create a FormData object to hold the form data, including the file var formData = new FormData(this); $.ajax({ url: '<?=$user_base_url?>/profil/save_directors', // Adjust the URL according to your setup type: 'GET', data: formData, processData: false, // Important! Prevent jQuery from automatically transforming the data into a query string contentType: false, // Important! Prevent jQuery from overriding the content type dataType: 'json', success: function(response) { if (response.status === 'success') { alert('Director saved successfully!'); // Optionally, redirect or refresh the page } else { alert('Failed to save director.'); } }, error: function(xhr, status, error) { console.log('AJAX error:', error); alert('An error occurred while saving the director.'); } }); }); }); </code>
$(document).ready(function() {
    $('#directorsForm').on('submit', function(event) {
        event.preventDefault(); // Prevent the form from submitting the traditional way

        // Create a FormData object to hold the form data, including the file
        var formData = new FormData(this);
        $.ajax({
            url: '<?=$user_base_url?>/profil/save_directors', // Adjust the URL according to your setup

            type: 'GET',
            data: formData,
            processData: false,  // Important! Prevent jQuery from automatically transforming the data into a query string
            contentType: false,  // Important! Prevent jQuery from overriding the content type
            dataType: 'json',
            success: function(response) {
                if (response.status === 'success') {
                    alert('Director saved successfully!');
                    // Optionally, redirect or refresh the page
                } else {
                    alert('Failed to save director.');
                }
            },
            error: function(xhr, status, error) {
                console.log('AJAX error:', error);
                alert('An error occurred while saving the director.');
            }
        });
    });
});

i use get as the post return error.
i fill the form and upload an image but i have recieved :

You did not select a file to upload.

any one can help how should i resolve that?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật