简体   繁体   English

使用PHP访问javascript数组值

[英]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. 我已经在Javascript中创建了一个名为“ arrayModel”的数组,并且试图在PHP中访问这些值。 So as to store those values to PHP variables. 以便将这些值存储到PHP变量中。

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? 知道如何在PHP中访问数组的值吗?

Thanks in advance. 提前致谢。

PHP is a server language and javascript is a browser language. PHP是服务器语言,而javascript是浏览器语言。 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. 因此,无论您用JavaScript编写什么内容,都只会在浏览器中进行,只有当您使用AJAX将其发送到服务器时,PHP才能对其进行处理。

You can use AJAX to do that 您可以使用AJAX做到这一点

First, you should json stringify it 首先,您应该对它进行json stringify

var arrayModel= JSON.stringify(arrayModel);

Then pas it via AJAX 然后通过AJAX粘贴

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

Then in your PHP file access like 然后在您的PHP文件访问中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM