2

How can i get my object array serializable in jquery to php ?

I have this:

console log of array structure

I need get this array sent by jQuery(vetDespesas) in php to create my sql query.

I try use dump php serialize, unserialize but it won't work.

I use this in jQuery:

   // this = my Form html

    var dataSend = $(this).serializeArray(); // other datas...

    dataSend.push({name:'moeda',value:moeda});
    dataSend.push({name:'moedaCotacao',value:moedaCotacao});
    **dataSend.push({name:'vetDespesas',value:vetDespesas});** object array

and in php, how can I write the code?

$requisitadopor = $_POST['requisitadopor'];
$autorizadopor = $_POST['autorizadopor'];
$departamento = $_POST['departamento'];
$unidade = $_POST['unidade'];

var_dump($_POST['vetDespesas']);  // doesn't work =/ (array of objects)


vetDespesas = JSON.stringify(vetDespesas);

and i php use:

    $a = json_decode($_POST['vetDespesas']);
    var_dump($a[0]);

My chrome console print:

object(stdClass)[2] public 'dataDespesa' => string '15/12/2015' (length=10) public 'descDespesa' => string 'teste1' (length=6) public 'budgetDespesa' => string '001 001 0E2R' (length=12) public 'valorDespesa' => string '2133.33' (length=7)

How can I access this data?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
felipe muner
  • 357
  • 2
  • 6
  • 13
  • Please keep in mind that you can add images within stackoverflow, you don't need external pages for that (that can go dead and hence harm the question in the future). In this case, a copy paste of the console.log would have been a better choice. Also, keep your text concise and to the problem. – k0pernikus Dec 16 '15 at 00:43
  • thanks k0pernikus the next answers i will do it =) – felipe muner Dec 16 '15 at 01:14

1 Answers1

1

You should turn it into a JSON string before send it to PHP :

vetDespesas = JSON.stringify(vetDespesas);

And to get the object in PHP code you should use json_decode() :

$my_object = json_decode($_POST['vetDespesas']);

You can access to object(stdClass) attributes using ->, e.g :

echo $my_object->dataDespesa;
echo $my_object->budgetDespesa;

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • No =/ i use var_dump function and the return was:
    string '[object Object],[object Object],[object Object]' (length=47)
    
    – felipe muner Dec 15 '15 at 23:59
  • where you init `vetDespesas` in javascript ? – Zakaria Acharki Dec 16 '15 at 00:00
  • my vetDespesa was initialized in javascript and is correct.. var vetDespesas = criaVetorObjDespesa(); var dataSend = $(this).serializeArray(); dataSend.push({name:'vetDespesas',value:vetDespesas}); – felipe muner Dec 16 '15 at 00:03
  • i tried your updated but don't work. console google chrome: Notice: Array to string conversion in C:\wamp\www\britanicaBlueForm2\templatesHTML\php\adiantamento.php on line 34..... My line 34 => $a = json_decode($_POST['vetDespesas']); echo $a[0]; .... sorry i'm newbie in php rs – felipe muner Dec 16 '15 at 00:20
  • That true because `$a[0];` will return the first object in `vetDespesas` it's an array so you should use `print_r($a[0])` and not `echo`. – Zakaria Acharki Dec 16 '15 at 00:25
  • u are a hero hahahahah WORK =) i use : print_r($a[0]->dataDespesa); to test =) now what u tell me the best way to iterate this array object? i think that this is the best way: foreach ($a as $valor) { $sql = "insert into adiantamento values('".$valor->dataDespesa."','".$valor->budgetDespesa."','".$valor->valorDespesa."');" mysql($sql); } – felipe muner Dec 16 '15 at 00:35
  • where are u from? let go talking about developing =) you helped me a lot... when u come to brazil talk with me and i will introduce you to what this country have better hahahahaha – felipe muner Dec 16 '15 at 00:39