2

I have this Js/Ajax code:

<script type="text/javascript">
$(document).ready(function(){
$("#message").hide();
$("#addsliderimage").submit(function(e){
    e.preventDefault();
    dataString=$("#addsliderimage").serialize();
    $.ajax({
        type: "POST",
        url: "addsliderimage_go.php",
        cache: false,
        data: dataString,
        success: function(res){
            //$("#message").show();
            $("#message").html(res);
            $('#message').fadeIn('slow');
            if(res.indexOf("success")!=-1)
            {
                window.location.href = res.substr(8);
            }
        }
    });
});
});
</script>

when the form is submitted, the page with the form on doesn't change the page is just submits the data to the URL in the ajax code.

i know this works as im using it on other forms however on this particular form im uploading images/files so i need a way to set the enctype to multipart/form-data

Viper
  • 1,327
  • 2
  • 12
  • 33
charlie
  • 1,356
  • 7
  • 38
  • 76
  • FYI, this: `dataString=$("#addsliderimage").serialize();` and be written like this: `dataString=$(this).serialize();` because of where it is situated in your code (i.e. under the `$("#addsliderimage")` selector). – cssyphus Jul 29 '13 at 16:31

1 Answers1

0

If you want to upload the files using AJAX you propably should use the well-known "hack", a simple iframe with a simple form that is submitted not by AJAX and catch the load event of that iframe so you can get the result of the upload.

For the part you have doubt in, well, since you prevent the default event when the "presubmit" (to name it some way) function ends nothing happens (you know, the default event return "true" after the submission), so you need to tell before the closure you're passing to submit function that it is needed to be submitted, but you gotta handle whether it is being submitted twice or not.

Omar
  • 689
  • 3
  • 16