Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4xjbh's avatar

PHP Compact

When you pass data from one page to another using the php compact() function it formats the data as an array.

How do you access that array data from the second page and jquery?

0 likes
3 replies
d3xt3r's avatar
function A () {
    $a = 1;
    $b = 2;
    return compact('a','b');
}

// php
function b () {
    $data = A();
    $a = $data['a']; // and so on 
}

// jquery (assuming its finally sent and parsed as json)
var a = data.a;
4xjbh's avatar

I'm not sure how to use that.

return view('contacts.edit', compact('contact'));

How do I get the array values and keys from jquery.

$contact['first'] doesn't work i'm guessing because it is in session data that jquery cannot access directly

ohffs's avatar

In your javascript you'd do something like :

var first_contact = "{{ $contact['first'] }}";

Ie, use blade to pull the data out so JS can use it as if you'd typed it in.

1 like

Please or to participate in this conversation.