beny lim
beny lim

Reputation: 1304

Accessing javascript array values using PHP

I have created an array named "arrayModel" in Javascript and I am trying to access those values in PHP. So as to store those values to PHP variables.

var arrayModel = new Array(5);
arrayModel[0] = "one";
arrayModel[1] = "two";
arrayModel[2] = "three";
arrayModel[3] = "four";
arrayModel[4] = "five";

Any idea how I can access the values of the array in PHP?

Thanks in advance.

Upvotes: 0

Views: 33

Answers (2)

Abhinav
Abhinav

Reputation: 8168

You can use AJAX to do that

First, you should json stringify it

var arrayModel= JSON.stringify(arrayModel);

Then pas it via AJAX

$.ajax({
    type: 'POST',
    data: {yourData: arrayModel},
    dataType: 'html',
    url: 'yourPHPFile.php'
});

Then in your PHP file access like

$yourData =json_decode($_POST['yourData']); 

Upvotes: 1

OSDM
OSDM

Reputation: 275

PHP is a server language and javascript is a browser language. That means they are not working together. So whatever you write in the javascript will be only in the browser, only when you send it to the server with for example AJAX then PHP can do something with it.

Upvotes: 1

Related Questions