I'm trying to send all data from my form with Jquery and Ajax to a PHP REST webservice.
JS
$("#idform").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
//Get Data
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method');
/* Send the data using post and put the results in a div */
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
dataType: "json",
//processData: false, // tell jQuery not to process the data
//contentType: false // tell jQuery not to set contentType
success: function(res){
if (res.result == '0') {
$("#idform").html('<div class="text-lg">Good Job!</div>');
} else {
//$("#respuesta").html('Error:');
$("#respuesta").html(res.message);
}
},
error:function(res){
$("#respuesta").html('Error comunicating webservice');
}
});
});
PHP I'm doing something like this with files:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$fileuploaded = $_FILES['fileuploaded']['tmp_name'];
$destinationpath = $_SERVER['DOCUMENT_ROOT']."/path/".$_FILES['fileuploaded']['name'];
move_uploaded_file($fileuploaded, $destinationpath);}
Everythings go ok when I make POST from form directly to php file but not when I'm trying to send data with ajax. PHP web service recieves all inputs but no my two files :(
Sorry for bad written english!