Problems with AJAX and PHP

2

I'm in trouble because I'm passing a couple of parameters from AJAX to PHP but PHP does not collect this data.

function ImprimirFichaUsuario(){

    var id = $('#idusuarionuevo').val();
    var name = $('#nombrenuevo').val();

    //Estos alerts funcionan, sacan los valores, es decir el problema está mas adelante
    alert(id);
    alert(name);

    var params = {
        "id" : id,
        "noim" : name
    };
    $.ajax({
        type: "POST",
        url: "php/app/mantenimientos/phpcontrolusuarios/imprimir.ficha.php",
        data  : params,
        dataType: "json",
        encode: true
    })
    .done(function(data){
    ...
    });}

And in print.ficha.php I have this:

<?php  

$id = $_POST['id'];
$nombre = $_POST['noim'];

echo "ID: ";
echo $id;
echo "y nombre ";
echo $nombre;     <?

When I see that this file responds, I find that it does not receive the data correctly by POST. Any ideas? Thank you very much in advance Greetings, Ivan.

EDIT:

Thank you very much everyone! I already have it, I worked all the advice, especially the juank, but thanks to Kevin and OscarGarcia for recommending that other way. What helped me most was the console.log in response to AJAX. There I realized that if I collected the data well, and the one that treated them badly in PHP was me xD. Greetings and thanks !!!

    
asked by SrMcLister 20.08.2018 в 12:56
source

1 answer

1

In your ajax add JSON.stringify(params)

$.ajax({
    type: "POST",
    url: "php/app/mantenimientos/phpcontrolusuarios/imprimir.ficha.php",
    data  : JSON.stringify(params),
    dataType: "json",
    encode: true
})

and in your php

$parametros = json_decode(file_get_contents("php://input"));

And then you work it as an object in this way:

$id = $parametros->id;
$nombre = $parametros->noim;
    
answered by 20.08.2018 / 14:07
source