-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletmetest.php
More file actions
27 lines (25 loc) · 783 Bytes
/
Copy pathletmetest.php
File metadata and controls
27 lines (25 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<html>
<head>
<title>Pass JS array to PHP.</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<form id="testform" action="#" method="post">
<input type="hidden" name="myArray" value="">
<!-- other form fields go here -->
<input type="submit" value="Submit">
</form>
<script>
var myArray = ["apple", "banana", "cherry"];
var form = document.getElementById("testform");
var hiddenInput = form.querySelector('input[name="myArray"]');
hiddenInput.value = JSON.stringify(myArray);
</script>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$myArray = json_decode($_POST["myArray"]);
var_dump($myArray);
}
?>
</body>
</html>