Passing array html form

Submit an Array with a HTML Form

Mail in your door

11 August 2014 · 262 words

Most of us will know how HTML forms work: inputs contained within the form send data to a url, by either a GET or POST request. This will result in the server being able to access this data on the other end.

The way to do this usually goes something like this:

Interestingly, and to the point of this post, you can also post an array of data with a form by using square brackets. If you place something within the square brackets then the resulting array becomes associative, otherwise it’ll be numeric.

Checkboxes#

This method makes submitting names and address neater. However, one of the biggest differences is when using checkboxes, as they don’t need a different name for each input, one name, one array, multiple values — no more huge isset checking statements!

Читайте также:  Value python что значит

As you can see this is a much neater way of submitting data with forms, both from a html and a server side point of view.

Profile Pic of Edd

About the Author: Edd Turtle

Edd is a PHP and Go developer, specialising in building scalable web applications. He enjoys blogging about his experiences, mostly about creating and coding new things he’s working on and is a big beliver in open-source and Linux.

Источник

Post Array From HTML Form To PHP (Simple Examples)

Welcome to a tutorial on how to post an array from an HTML form to PHP. The “standard” HTML form fields are easy enough. Just add the name attribute to the fields, submit the form, and that’s it. But what if we want to post an array instead?

To post an array from an HTML form to PHP, we simply append a pair of square brackets to the end of the name attribute of the input field. For example:

But how about doing this with AJAX? How about multi-dimensional arrays? How do we handle the post data in PHP? That is what we will walk through in this guide, with examples. Read on to find out!

TLDR – QUICK SLIDES

How To POST An Array From HTML Form To PHP

TABLE OF CONTENTS

HTML POST ARRAY TO PHP

All right, let us now get into the various examples of posting an array to a PHP script.

EXAMPLE 1) POST A SIMPLE ARRAY TO PHP

1A) THE HTML

As in the introduction, we only need to append square brackets [] behind the name to POST arrays to the PHP script.

1B) SERVER-SIDE PHP

Yep, this should be straightforward enough. Since we defined colors[] , $_POST[«colors»] will be an array.

EXAMPLE 2) POST NESTED ARRAY TO PHP

2A) THE HTML

Multi-dimensional array fields are not that difficult either – Just use 2 square brackets, and give the first one a key. That’s it, done.

2B) SERVER-SIDE PHP

No mystery here either – $_POST[«fav»] is a multi-dimensional array as expected.

EXAMPLE 3) POSTING ARRAY WITH AJAX

3A) THE HTML

Not much of a difference here – Still the same HTML form, but using onsubmit=»return save()» to use Javascript AJAX to handle the submission instead.

3B) THE JAVASCRIPT

function save () < // (A) GET FORM DATA var form = document.getElementById("myForm"), data = new FormData(form); // (B) TO MANUALLY APPEND MORE DATA data.append("address[]", "Foo Street 123"); data.append("address[]", "Hello World #234"); // (C) AJAX FETCH fetch("3c-AJAX.php", < method:"POST", body:data >) .then(res => res.text()) .then(res => < console.log(res); // DO SOMETHING >); return false; >
  1. Collect data from the HTML form.
  2. Optional, we can manually append more fields to the data.
  3. Lastly, send the data to the server, and read the server response as text.

3C) SERVER PHP

Nothing to see here… Just a dummy script that will echo whatever is being submitted.

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

SUPPORT

600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.

EXAMPLE CODE DOWNLOAD

Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

That’s all for this project, and here is a small section on some extras that may be useful to you.

ONLY FOR PHP!

Before the toxic trolls get angry – Take note that the above method of name=»something[]» works for PHP, but it can be different for other languages. For example, some languages may require all the name attribute to be the same instead:

JSON ENCODE-DECODE

To better support cross-platform data exchange, the common industry practice is to turn the array into a flat string, using JSON encode:

  

On the server side, we can do a JSON decode to get the array back.

P.S. Any programming language can encode-decode a JSON string so long as there is a library or parser.

YOUTUBE TUTORIAL

INFOGRAPHICS CHEAT SHEET

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment Cancel Reply

Breakthrough Javascript

Take pictures with the webcam, voice commands, video calls, GPS, NFC. Yes, all possible with Javascript — Check out Breakthrough Javascript!

Socials

About Me

W.S. Toh is a senior web developer and SEO practitioner with over 20 years of experience. Graduated from the University of London. When not secretly being an evil tech ninja, he enjoys photography and working on DIY projects.

Code Boxx participates in the eBay Partner Network, an affiliate program designed for sites to earn commission fees by linking to ebay.com. We also participate in affiliate programs with Bluehost, ShareASale, Clickbank, and other sites. We are compensated for referring traffic.

Источник

Оцените статью