Jump to content

Recommended Posts

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>

 

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
                )

        )

)

 

Anybody?

 

Thanks.

 

Carlos

Link to comment
https://forums.phpfreaks.com/topic/181534-who-creates-form-arrays-html-or-php/
Share on other sites

Well, you've hard coded the form elements names in an array format (you could get php to generate it (it'd look the same as you have it)). Then when the data comes back to the server, it (not too sure here) passes the data to php which parses the data as an array (or the server parses the data and then passes it to the php engine) (either way server side).

 

So, in reality php 'handles' the array, and the html is just html...

 

Helped or not?

Thanks rarebit but I am still unclear. 

 

I understand that the server receives stuff and then hands it over to the PHP interpreter for handling.  In my case all files ending in .php. 

 

What I am unclear on is whether the array construct that you see in my HTML form is a capability of HTML itself or whether the array constructs in the form I posted only work because PHP reads the HTML and processes them as arrays? 

 

In other words does the HTML have an ability to return POST variables of an array kind irrespective of whether an HTML form is processed through PHP, Perl, or any other scripting language or is it a matter of a scripting language reading it as such but where HTML itself does not allow array constructs by itself. 

 

If I had the time I would see if the same thing could be possible with Perl but I haven't fooled with Perl in a long, long time. 

 

I hope my question makes sense. 

 

Carlos

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.