0

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!

0

1 Answer 1

0

I think you have to use the JS like this

JS

$.ajax({
        url: "web_service.php",
        data:'value1=1&value2='+$("#num").html()+'&value3=G',
        cache: false,
        async : true,
        type: "POST",
        ....

In doc.php you have the code to connect to web service in PHP and the parameters send in the data line above. The data is an example

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.