[英]How to pass JavaScript variables to PHP?
I need extract the 24 numbers generated in this array and post to php for: $number1=;我需要提取此数组中生成的 24 个数字并发布到 php 以获得: $number1=; ... $number24=;
... $number24=; What's the best practice?
最佳做法是什么?
var usedNums = new Array(76);
function newCard() {
for (var i = 0; i < 24; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var newNum;
var colPlace = new Array(0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4);
do {
newNum = (colPlace[thisSquare] * 15) + getNewNum() + 1;
}
while (usedNums[newNum]);
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
}
function getNewNum() {
return Math.floor(Math.random() * 75);
}
function anotherCard() {
for (var i = 1; i < usedNums.length; i++) {
usedNums[i] = false;
}
newCard();
}
Source: https://codepen.io/koriwade/pen/BoOqGM资料来源: https://codepen.io/koriwade/pen/BoOqGM
Many Thanks非常感谢
You can create a JSON object like {"num1":"value1","num2":"value2"}
您可以像
{"num1":"value1","num2":"value2"}
The pass it to PHP via ajax request.通过 ajax 请求将其传递给 PHP。
On PHP side you will receive an array once you json_decode
在 PHP 一侧,您将收到一个数组,一旦您
json_decode
and then use extract
function to map the array key as variables.然后使用
extract
function 到 map 数组键作为变量。
You cannot directly pass the js variables to php because the php runs on server side.您不能直接将 js 变量传递给 php,因为 php 在服务器端运行。 But there are several ways of passing variables from JavaScript to PHP.
但是有几种方法可以将变量从 JavaScript 传递到 PHP。
With the help of ajax you can pass js variable to the PHP.在 ajax 的帮助下,您可以将 js 变量传递给 PHP。
You may use JSON.stringify( var );
您可以使用
JSON.stringify( var );
to send var
to the server in a request在请求中将
var
发送到服务器
And Decode that afterwards, with json_decode($var,true);
然后解码,用
json_decode($var,true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.