-3

I have in Javascript my array and ajax call:

righe = [];
righe.push({
  ragione_sociale:  $('#ragione_sociale').val(),
  via:                      $('#via').val(),
  cap:                  $('#cap').val(),
  localita:             $('#localita').val(),
  provincia:            $('#provincia').val(),
  telefono:         $('#telefono').val(),
  fax:          $('#fax').val(),
  settore:          $('#settore').val(),
  attivita:         $('#attivita').val(),
  note:         $('#note').val()
 });

$.ajax({
    type: "POST",
    url: "/ajaxcall/inserisciAzienda.php",
      data: {righe : righe},
  success: function(data){

            console.log("okkk "+data);

    }......

and this is my inserisciAzienda.php:

<?php

  $dati = array ($_POST["righe"]);

  echo "result: ".$dati[0];

?>

but I have the following error:

 okkk <br />
 <b>Notice</b>:  Array to string conversion in <b> /Applications/
 XAMPP/xamppfiles/htdocs/app/badges/ajaxcall/inserisciAzienda.php</b> 
 on line <b>11</b><br />result: Array

I'm not able to get the array on the php file.

glan84
  • 1
  • 1
  • What do you get when you do `print_r($dati[0]);` instead? – Anthony Mar 14 '18 at 20:19
  • 1
    `$dati[0]` (which is really just `$_POST["righe"]`) is an array. What exactly are you trying to do? What do you expect/want the result to be? – Patrick Q Mar 14 '18 at 20:20
  • 1
    Possible duplicate of [Pass Javascript Array -> PHP](https://stackoverflow.com/questions/5035547/pass-javascript-array-php) – Nic3500 Mar 14 '18 at 20:20
  • I am sure you need to serialize your array at the time of passing – Naveed Ramzan Mar 14 '18 at 20:22
  • try echo "result: ".$dati[0]['ragione_sociale']; – ScaisEdge Mar 14 '18 at 20:22
  • 2
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Liora Haydont Mar 14 '18 at 20:28
  • I have to extract received data and send them to a c# web service like this: [WebMethod] public bool ScriviAziende(string ragione_sociale, string via, string cap, string localita, string provincia, string telefono, string fax, string settore, string attivita, string note) – glan84 Mar 14 '18 at 20:42

2 Answers2

0

echo can't print non-scalar variables (objects, arrays). This can be done with print_r. With your code:

$dati = array ($_POST["righe"]);

echo "result: ";
print_r($dati[0]);
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • I have to extract received data and send them to a c# web service like this: [WebMethod] public bool ScriviAziende(string ragione_sociale, string via, string cap, string localita, string provincia, string telefono, string fax, string settore, string attivita, string note). So i have get the strings. – glan84 Mar 14 '18 at 21:15
  • Getting settings isn't the same as printing them. Do you know how to get values from an array? – Anthony Mar 14 '18 at 21:23
0

Ok,

I solved with

$dati = $_POST["righe"];

instead

$dati = array($_POST["righe"]);

and getting values in this way:

echo $dati[0]['ragione_sociale'];
glan84
  • 1
  • 1