1

I am having the code with the below format.

PHP file :

<form action="http://clientwebapi.com/createEvent" id="form_createEvent" method="post" enctype="multipart/form-data">
<input type="text" name="image_title" />
<input type="file" name="media" accept="image/*,video/*"/>
</form>

JQUERY file:

$('#form_createEvent').submit(function () {
    var form = $(this);
    $.ajax({
        url: form.attr("action"),
        type: form.attr("method"),
        xhrFields: {
            withCredentials: true
        },
        data: form.serialize()
    }).done(function () {
        showCurrentLocation();
        alert('Event created successfully..');
        location.reload();

    }).fail(function () {
        alert("fail!");
    });
    event.preventDefault();
});

The above Jquery code is submitting. Also While I am submitting the below format, It will redirect to the "http://clientwebapi.com/createEvent" and Event created successfully.

Form Submit and redirect to client page:

$('#form_createEvent').submit(function () {
    var fd = new FormData();
    fd.append('media', input.files[0]);

    $.ajax({
        url: form.attr("action"),
        data: fd,
        processData: false,
        contentType: false,
        type: form.attr("method"),
        success: function (data) {
            alert(data);
        }
    });
event.preventDefault();

});

Here How can I prevent while submit the form and create the event with the given image. Kindly help

3
  • do return false; in the end after $.ajax() Commented Aug 28, 2013 at 9:56
  • Or do it the right way Commented Aug 28, 2013 at 10:18
  • Actually I am sending the datas including image to "clientwebapi.com/createEvent". After processing this, it will send some response. I need to know how can we pass image to client API via this jquery, ajax code Commented Aug 28, 2013 at 10:38

3 Answers 3

1

You forgot to add event as argument to the submit function:

$('#form_createEvent').submit(function (event) {
Sign up to request clarification or add additional context in comments.

1 Comment

That's because you have event.preventDefault(); after your ajax code.
1

I found the Answer for this. I made some mistake here. I resolved by using the below code..

$('#form_createEvent').submit(function() { 
            var form = new FormData($(this)[0]); 
            $.ajax({
                url: 'http://clientwebapi.com/createEvent/',
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                data: form,
                beforeSend: function () {
                    $("#output_process").html("Uploading, please wait....");
                },
                success: function () { 
                    $("#output_process").html("Upload success.");
                },
                complete: function () {
                    $("#output_process").html("upload complete.");
                },
                error: function () {
                    //alert("ERROR in upload");
                    location.reload();
                }
            }).done(function() { 
                alert('Event created successfully..');

            }).fail(function() {
                alert("fail!");
            });
            event.preventDefault();
        });

Comments

0

you can stop the form submission by return false;

$('#form_createEvent').submit(function () {
    // some code
    $.ajax({
        // some code/objects
    });

    return false; // here, it will stop the form to be post to the url/action

});

2 Comments

Sorry @GARB i didn't notice that you are trying to upload file, what you actually need is to check this malsup.com/jquery/form
either use this code malsup.com/jquery/form/#ajaxForm or this code malsup.com/jquery/form/#ajaxSubmit directly into your code and see the magic.

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.