i have create a form to send data and file with jquery ajax. This code fork with firefox but the upload don't work in chrome and other browser.
this is my code: the html
<form method="post" enctype="multipart/form-data" id="form_foto">
<table>
<tr>
<td class="form_label">
<label for="nome">Nome</label>
</td>
<td class="form_input">
<input type="text" name="nome" value="" id="nome" />
</td>
</tr>
<tr>
<td class="form_label">
<label for="cognome">Cognome</label>
</td>
<td class="form_input">
<input type="text" name="cognome" value="" id="cognome" />
</td>
</tr>
<tr>
<td class="form_label">
<label for="email">Email</label>
</td>
<td class="form_input">
<input type="text" name="email" value="" id="email" />
</td>
</tr>
<tr>
<td class="form_label">
<label for="personaggio">Personaggio</label>
</td>
<td class="form_input">
<input type="text" name="personaggio" value="" id="personaggio" />
</td>
</tr>
<tr>
<td class="form_label">
<label class="form_label_upload" for="file">UPLOAD FOTO</label>
</td>
<td class="form_input">
<div class="porel">
<span class="file-wrapper">
<input type="file" name="file-0" id="file" />
<span class="button"></span>
</span>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="footer_dialog">
<p><input type="submit" id="submit_form" value="" /></p>
the js code:
(function ($) {
$.performAjaxSubmit = function() {
var nome = document.getElementById("nome").value;
var cognome = document.getElementById("cognome").value;
var email = document.getElementById("email").value;
var personaggio = document.getElementById("personaggio").value;
var exist = document.getElementById("exist").value;
var fb_id = document.getElementById("fb_id").value;
var first_name = document.getElementById("first_name").value;
var last_name = document.getElementById("last_name").value;
emailExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
if (nome.length == 0 ) {
$("div.errore").html("Inserisci il nome");
return false;
}
if (cognome.length == 0 ) {
$("div.errore").html("Inserisci il cognome");
return false;
}
if (personaggio.length == 0 ) {
$("div.errore").html("Inserisci il personaggio");
return false;
}
if (email.length == 0 || !emailExp.test(email) ) {
$("div.errore").html("Inserisci l'email");
return false;
}
var formdata = new FormData();
formdata.append("nome", nome);
formdata.append("cognome", cognome );
formdata.append("email", email);
formdata.append("personaggio", personaggio);
var dim = $('input[type="file"]').length;
for(j=0;j<dim;j++){
$each($('input[type="file"]')[j].files, function(i, file) {
formdata.append('file-'+j, file);
});
}
var xhr = new XMLHttpRequest();
xhr.open("POST","index.php/upload/index_upload", true);
xhr.send(formdata);
$("div#dialog_content").html("<img src='<?php echo base_url(); ?>asset/public/images/loading.gif'/>");
xhr.onload = function(e) {
if (this.status == 200) {
$("div#dialog_content").html(this.responseText);
//alert(this.responseText);
}
else{
alert(this.responseText);
}
};
};
Works well with firefox, on chrome and safari have problem. Any idea?
How i can send data and file with ajax? I took the code from this article
The only way to upload files via "ajax" on Internet Explorer is using iframes, while other browsers support upload via ajax. Your best approach would be to use a plugin, like suggested.
Personally, I use jQuery Form Plugin http://malsup.com/jquery/form/