gpt4 book ai didi

javascript - 从 php 文件访问 javascript 数组

转载 作者:行者123 更新时间:2023-11-28 15:51:42 26 4
gpt4 key购买 nike

如何将 JavaScript 数组 vArray 传递到 File.php ,并从 vArray 检索两个值。

我尝试过:

<input type="button" id="button" onClick = "send_V();" >

<script>
// Creat Array with Values from checkboxes
$('input[type=checkbox]:checked').each(function() {
vArray.push($(this).val());
});

// Set array to only 2 values ( disable checkboxes)
var n = $('input[type=checkbox]:checked').length >= 2;
$('input[type=checkbox]').not(':checked').attr('disabled',n);

// Send array to File.php where I can manipulate its value1, and value2 to query db
function send_V(vArray)
{
$.ajax({
type: "POST",
url: "File.php",
beforeSend: function () {
$("#result").html("<option>Loading ...</option>");
},
data: "vArray="+vArray,
success: function(msg){
$("#result").html(msg);
}
});
}
</script>

在 php 端(File.php)

$value = $_POST['vArray'];
var_dump(vArray);

但我无法也不知道如何操作 javascript 变量。有人可以告诉我一个简单有效的方法吗?这个逻辑有什么问题吗?谢谢

最佳答案

使用json。在 js 中对数组进行编码 ( How do I encode a javascript object as JSON? ),在 php 中对其进行解码 ( http://php.net/manual/ro/function.json-decode.php )。

关于javascript - 从 php 文件访问 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com