1

I am trying to upload an image to database from html page in a form in a modal through php & ajax,but it doesn't work i don't know why,anyone can help me?

HTML Page

<form method="post" enctype="multipart/form-data">
    <div class="row">
      <div class="col-sm-4">
        <label class="Modallabel">Photo:</label>
      </div>
      <div class="col-sm-8">
        <div class="input-group input-group-sm" style="margin-top: -5px;">
            <input type="file" id="fileUpload" name="fileUpload" style="color: #ffffff;"/>
        </div><br>
      </div>
    </div>
</form>
<button type="button" class="btn btn-success btn-md" style="margin:0;width: 75px;" onclick="AddNewCustomer()">Add</button>

Javascript Function

function AddNewCustomer()
{
//Image upload
$(document).ready(function() 
{
    $(document).on('change', '#fileUpload', function() //NOT WORKING HERE
    {
        var property = document.getElementById("fileUpload").files[0];
        var image_name = property.name;
        var image_extension = image_name.split('.').pop().toLowerCase();
        if (jQuery.inArray(image_extension, ['gif','png','jpg','jpeg']) == -1)
        {
            alert("invalid Image File");
        }
        var image_size = property.size;
        if(image_size > 2000000)
        {
            alert("Image File Size is very big");
        }
        else
        {
            var form_data = new FormData();
            form_data.append("fileUpload", property);

            $.ajax
            ({
                type:"POST",
                url:"addNewCustomer.php",
                processData: false,
                cache: false,
                contentType: false,
                data:
                    {
                    'form_data':form_data
                    },
                success: function(data){
                    if (data == "Success!")
                    {
                        sweetAlert("Data entered successfully!");
                    }
                    else if(data == "Exist") 
                    {
                        sweetAlert("","Customer already exists!","error");
                        return false;
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log('ERROR', textStatus, errorThrown);
                }
            })
        }
    })
})

}
2
  • doesn't work is not enough to understand what is going on , 2nd: where is your php code? Commented May 23, 2017 at 21:04
  • it means that the code doesn't enter to run the next code between parentheses. Commented May 23, 2017 at 21:07

2 Answers 2

1

change event would be attached after click at <button> at onclick="AddNewCustomer().

You can remove AddNewCustomer function and call change on #fileUpload element at click of .btn element.

$(document).ready(function() {
  $(document).on('change', '#fileUpload', function() {//do stuff})

  $(".btn").on("click", function() {
    $("#fileUpload").trigger("change");
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

The form has another inputs (First Name,Last Name,Email,...Photo) that's why i have to have the function AddNewCustomer
@NewDeveloper What is purpose of click event at .btn element?
The purpose is to add the customer data in the database(Name,Phone,Emai,....Photo)
@NewDeveloper Does approach at Answer not produce expected result?
0

You want to upload the image immediately after it's chosen, but you only run the function when the 'Add' button is clicked.

You need to take your change code out of the function, or invoke the function when the page is loaded to add the listeners to the form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.