Who creates form arrays? HTML or PHP?

Discussion in 'PHP' started by carlos123, Nov 14, 2009.

  1. #1
    I just discovered today that I can apparently create arrays in my HTML forms but I have not seen official documentation on this anywhere and am wondering if this feature is a PHP capability (where PHP parses the HTML in the .php file and renders the array constructs into arrays) or whether the capability in question is a feature of just HTML (irrespective of what PHP does with it).

    Here is some code that shows what I am asking about...

    
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    	echo "<pre>";
    	print_r($_POST);
    	echo "</pre";
    }
    ?>
    <form action="" method="POST">
    	<input type="submit" name="submit" value="Submit">
    	<br>
    	<input type="text" name="field_set[1][sort]" value="1">
    	<input type="text" name="field_set[1][link_text]" value="About Me">
    	<input type="text" name="field_set[1][link_points_to]" value="about-me.txt">
    	<input type="text" name="field_set[2][sort]" value="2">
    	<input type="text" name="field_set[2][link_text]" value="Home">
    	<input type="text" name="field_set[2][link_points_to]" value="home.txt">
    	<input type="text" name="field_set[3][sort]" value="3">
    	<input type="text" name="field_set[3][link_text]" value="Privacy Policy">
    	<input type="text" name="field_set[3][link_points_to]" value="privacy-policy.txt">
    </form>
    
    Code (markup):
    The output looks like this...

    
    Array
    (
        [submit] => Submit
        [field_set] => Array
            (
                [1] => Array
                    (
                        [sort] => 1
                        [link_text] => About Me
                        [link_points_to] => about-me.txt
                    )
    
                [2] => Array
                    (
                        [sort] => 2
                        [link_text] => Home
                        [link_points_to] => home.txt
                    )
    
                [3] => Array
                    (
                        [sort] => 3
                        [link_text] => Privacy Policy
                        [link_points_to] => privacy-policy.txt
                    )
    
            )
    
    )
    
    Code (markup):
    Anybody?

    Thanks.

    Carlos
     
    carlos123, Nov 14, 2009 IP
    ShadyStudent likes this.
  2. ShadyStudent

    ShadyStudent Peon

    Messages:
    341
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Very good question and this is my understanding.

    When you click a link or make a request to the server it can be different types of requests. We are intertested in when you submit a form using either a POST/GET method. When you make this request it makes a POST/GET HTTP request (nothing to do with PHP yet). When this request is made, HTTP encodes key-value pairs in the request something like this:

    Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
    Code (markup):
    This can appear in the URL for GET requests, but this is hidden for POST.

    POST/GET are requests made to the server via HTTP - the $_POST/$_GET in PHP are just data captured from this requests. Both $_POST/$_GET are contained in $_REQUEST.

    I really like it when people ask deeper questions like this rather than how can I do x in this language! Repped.
     
    ShadyStudent, Nov 14, 2009 IP
  3. carlos123

    carlos123 Peon

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hmm...interesting. You gave me an idea ShadyStudent.

    I changed the method to GET in my code and came up with this line in the URL.

    
    http://learnphp/form-arrays.php?action[submit]=Submit&field_set[1][sort]=1&field_set[1][link_text]=About+Me&field_set[1][link_points_to]=about-me.txt&field_set[2][sort]=2&field_set[2][link_text]=Home&field_set[2][link_points_to]=home.txt&field_set[3][sort]=3&field_set[3][link_text]=Privacy+Policy&field_set[3][link_points_to]=privacy-policy.txt
    
    Code (markup):
    http://learnphp is just a local named domain I use on my laptop to test things. It's not a real domain that is accessible over the internet.

    form-arrays.php is just the name of my fooling around script to try out...well...form arrays.

    What this tells me is that the turning of the array constructs in the HTML form that I posted into arrays is actually a feature of the HTTP protocol and is not a PHP thing at all. PHP just receives what the HTTP protocol gives it (with a couple of exceptions like that periods are allowed in HTML form variable names but PHP converts then to '_' characters).

    Odd that I have never, ever read about this in any of the countless HTML guides and tutorials I have ever read. Very odd. It's turning out to be such an incredibly useful feature that is allowing me to streamline and otherwise clean up sections of my PHP code.

    Carlos
     
    carlos123, Nov 14, 2009 IP
  4. rachusrs

    rachusrs Guest

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    creating form elements in html and processing it with php (get,post,request ) is good form of coding and it is easy and simple
     
    rachusrs, Nov 15, 2009 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Technically, HTML is creating several unique form elements and there is no association between them unless they have the exact same name.

    It is up to things such as PHP, ASP, JS, etc to interpret the [] syntax of the form element names into an array type data structure.
     
    joebert, Nov 15, 2009 IP