Guillermo
Guillermo

Reputation: 326

passing multiple parameters using $.ajax by POST to php function

I have to pass data through POST, to a function in PHP, the problem is they don't retrieve the data.

var jsonText = JSON.stringify(origen);
var jsonTextDestino = JSON.stringify(destino);
$.ajax({
    type: "POST",
    url:"/lie/controlador/manejo_de_archivos/controlador.php?action=copiar_archivo",
    data: "origen=" + jsonText + "&destino=" + jsonTextDestino ,
    async: false,
    dataType: "json",
    success: function (jsondata) {
    }

controller Side in PHP

if ($_GET["action"] == "copiar_archivo"){
    echo json_encode($controlador-> copiar_archivo($_POST["origen"],    $_POST["destino"]));
}

function in PHP, model.

function copiar_archivo($path_o, $path_dest){
    //some code
}

I don't know if i am clear.

Upvotes: 1

Views: 2662

Answers (2)

CodeGodie
CodeGodie

Reputation: 12132

your ajax code should be rebuilt this way: (Notice the object being sent as your data parameters)

var my_object = {"origen": origen, "destino":destino};
$.ajax({
    type: "POST",
    url:"/lie/controlador/manejo_de_archivos/controlador.php?action=copiar_archivo",
    data: my_object ,
    async: false,
    dataType: "json",
    success: function (jsondata) {
    }
)}

Also, in your PHP, you don't need to use GET since your ajax is sending a POST request. Thus making GET['action'] irrelevant.

Upvotes: 2

Buddhi741
Buddhi741

Reputation: 375

var form = new FormData();
form.append("key1", "val1");
form.append("key2", "val2");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://test.com/php.php",
  "method": "POST",
  "headers": {},
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

try something like this code m8 with should help you out

Upvotes: 1

Related Questions