Using Hierarchical Structure in POST or GET When Submit Form in HTML
First, let's see a simple example:
File1: post.html
File2: post.php
When we visit post.html and input 'a' in the input box, then submit. We get the following prints:
Input 'a', 'b', 'c' in the text box respectively. And click 'submit', then we can get the following prints:
If we change the form method to 'post', we can get the similar results.
So the symbol '[ ]' in the attribute 'name' makes a sub array in post or get data. When we change 'word[a]' to 'word[a][c]', we get:
This character has been used in Word Press to update settings when we need to include multi items in a single option.
File1: post.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="post.php" method="get">
<input type="text" name="word" value="" />
<input type="submit" value="submit" />
</form>
</body>
</html>
File2: post.php
<?php
var_dump($_POST);
var_dump($_GET);
When we visit post.html and input 'a' in the input box, then submit. We get the following prints:
/Library/WebServer/Documents/test/post.php:2:
array (size=0)
empty
/Library/WebServer/Documents/test/post.php:3: array (size=2) 'word' => string 'a' (length=1)
This is a basic example and can be found in almost all of the tutorials. But sometimes we want to do something more complex and need hierarchical array in post data. We can modify the example above:
File1: post.html
<!DOCTYPE html><html><head></head><body><form action="post.php" method="get"><input type="text" name="word[a]" value="" /><input type="text" name="word[b]" value="" /><input type="text" name="sentence" value="" /><input type="submit" value="submit" /></form></body></html>
Input 'a', 'b', 'c' in the text box respectively. And click 'submit', then we can get the following prints:
/Library/WebServer/Documents/test/post.php:2:
array (size=0)
empty
/Library/WebServer/Documents/test/post.php:3: array (size=2) 'word' => array (size=2) 'a' => string 'a' (length=1) 'b' => string 'b' (length=1) 'sentence' => string 'c' (length=1)
If we change the form method to 'post', we can get the similar results.
So the symbol '[ ]' in the attribute 'name' makes a sub array in post or get data. When we change 'word[a]' to 'word[a][c]', we get:
/Library/WebServer/Documents/test/post.php:2:
array (size=0)
empty
/Library/WebServer/Documents/test/post.php:3: array (size=2) 'word' => array (size=2) 'a' => array (size=1) 'c' => string 'a' (length=1) 'b' => string 'b' (length=1) 'sentence' => string 'c' (length=1)
This character has been used in Word Press to update settings when we need to include multi items in a single option.
评论
发表评论