Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/08/2019, 03:44
helenp
 
Fecha de Ingreso: mayo-2009
Mensajes: 382
Antigüedad: 15 años
Puntos: 6
Enviar array usando fetch a php

Hola
Hago fetch y funciona perfectamente, pero cuando quiero pasar un array usando getElementbyName pues no funciona.

El html del array:
Código HTML:
Ver original
  1. var inputNode = document.createElement("input");
  2.         inputNode.setAttribute('type', 'text');
  3.         inputNode.setAttribute('name', 'pers[]');
  4.         inputNode.setAttribute('id', 'persx');
  5.         inputNode.classList.add('granadainput');

El fetch javascript:
Código Javascript:
Ver original
  1. var nombresgranada = document.getElementsByName('pers[]');
  2.       fetch('/pasarelatours/incl-booking.php', {
  3.         method: 'POST',
  4.         headers: {
  5.           'Content-Type': 'application/json'
  6.         },
  7.         body: JSON.stringify({
  8.           lastInsertId: lastInsertId, language: language, nombresgranada: nombresgranada
  9.         })
  10.       })
  11.       .then(function (response) {
  12.         return response.text();
  13.       })
  14.       .then(function (body) {
  15.         document.getElementById('test').innerHTML = body;
  16.       });

Y lo recibo en el php así:
Código PHP:
Ver original
  1. $json_str = file_get_contents('php://input');
  2. $json_obj = json_decode($json_str);
  3. $language = $json_obj->language;
  4. $nombresgranada  = $json_obj->nombresgranada;

Y cuando intento meter la variable $nombresgranada en la base de datos obtengo este error:
Catchable fatal error: Object of class stdClass could not be converted to string in
Gracias