Welcome to WebmasterWorld Guest from 3.227.233.78

Forum Moderators: open

Send html array to php with fetch

     
4:07 pm on Aug 8, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Hello,
I do below fetch and it works perfect with normal parameters, however when I try to pass an array using getElementbyName it does not work.

Not sure if this should go here or in the php forum.

The javascript html form
var inputNode = document.createElement("input");
inputNode.setAttribute('type', 'text');
inputNode.setAttribute('name', 'pers[]');
inputNode.setAttribute('id', 'persx');
inputNode.classList.add('granadainput');



The javascript:
var nombresgranada = document.getElementsByName('pers[]');
fetch('/pasarelatours/incl-booking.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
lastInsertId: lastInsertId, language: language, nombresgranada: nombresgranada
})
})
.then(function (response) {
return response.text();
})
.then(function (body) {
document.getElementById('test').innerHTML = body;
});


Then I receive in the php file like this:
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$language = $json_obj->language;
$nombresgranada = $json_obj->nombresgranada;


And when I try to insert in database or print out the parameter $nombresgranada I get this error:
Catchable fatal error: Object of class stdClass could not be converted to string in
Thanks
5:48 pm on Aug 8, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 285


$json_obj->nombresgranada is, itself a class, so you need to access its individual members. You can do a
var_dump($json_obj->nombresgranada);
to identify exactly which members are populated.

Now, it might be more convenient to convert $json_str into an associative array.
$json_array=json_decode($json_str,true);

Then you can
echo '<pre>'; print_r($json_array); echo '</pre>';
to see exactly what you have.

edit: do not forget to control the data you are receiving, to avoid exploits, or malicious code.
7:04 pm on Aug 8, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Thanks,
I don't get this.

This is what var_dump($json_obj->nombresgranada); prints out
object(stdClass)#3 (2) { ["0"]=> object(stdClass)#4 (1) { ["_fs"]=> int(1345) } ["1"]=> object(stdClass)#5 (1) { ["_fs"]=> int(1350) } }

and this is echo '<pre>'; print_r($json_array); echo '</pre>';
Array
(
[lastInsertId] => 29
[language] => en
[nombresgranada] => Array
(
[0] => Array
(
[_fs] => 1345
)

[1] => Array
(
[_fs] => 1350
)
)
)

The array nombresgranada should contain the words, test and Helena

I have ever used fetch before, and its strange,
I have another fetch done exactly the same way, and in the php I need to print the result out like this: echo json_encode($id);

However on this Stripe I just do echo on the normal parameters without encoding, maybe it's due that this fetch is inside a Stripe function?
7:34 am on Aug 9, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Forget about this I said before (can't edit anymore):
"I have another fetch done exactly the same way, and in the php I need to print the result out like this: echo json_encode($id);
However on this Stripe I just do echo on the normal parameters without encoding, maybe it's due that this fetch is inside a Stripe function?"
The cases are different for both
4:04 pm on Aug 9, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Nov 27, 2003
posts: 1318
votes: 0


Finally I managed to send the array to php doing this:
var nombresgranada = [].slice.call(document.getElementsByName("pers[]")).map(input => input.value);