jems jems - 11 months ago 64
jQuery Question

How to debug a non-working response from AJAX in CodeIgniter?

I want to upload photo with jquery-ajax in CodeIgniter. First I want to pass file to my controller for just check my AJAX call properly working or not.
My code is not posting anything to my controller. I posting my code here, please show me my fault

Here is my code

jQuery and my input of my view is here (profile_view.php)

<script type="text/javascript">
$(document).ready(function() {
$("#file1").on("change", (function() {
$("#showimage").fadeIn("slow").html('Plaese Wait...');
target: '#showimage',
success: function(response) {
error: function(err) {

My input code is here

<div id="photoframe">
<form name='frm1' id="frm12" enctype="multipart/form-data" method="post" action="upload_photo">
<input type="file" name="file1" id="file1" style="visibility:hidden" />
<a style="color:white"><i class="fa fa-edit" id="img1">Edit Photo</i></a>
<div id="showimage" name='showimage'>

My controller is Here (upload_photo.php)

class Upload_photo extends CI_Controller {
function __construct() {
$this - > load - > helper(array('form', 'url'));
function index() {
$config['upload_path'] = '/user';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
if (isset($_POST['frm1'])) {
echo 'Image Uploaded';
echo "post".$_FILE['frm1'];
} else {
echo 'Image Upload Failed';

My output is :
Image Upload Failed

Answer Source

I think there is problem with your request, You have not passed any data with that request, You can use following data method to pass data with ajax:-

     url: "post.php",
     data: {
        id: 123
     type: "GET",
     dataType : "json",
     success: function( json ) {
        // Do your code here for success
     error: function( xhr, status, errorThrown ) {
        alert( "Sorry, there was a problem!" );
     complete: function( xhr, status ) {
        alert( "The request is complete!" );

And you can not send image file with these AJAX request.

Please change your PHP code also, something like following :-

if (isset($_FILES['frm1'])) {
   // Do your upload code here
    echo 'Image Upload Failed';