HTML PHP

HOW TO POST ARRAY FROM HTML TO PHP

(quick examples)

APPEND NAME[] <form method="post">   <input type="text" name="colors[]">   <input type="text" name="colors[]">   <input type="submit" value="GO"> </form>

$_POST["COLORS"] IS AN ARRAY print_r($_POST["colors"]);

SEND SIMPLE ARRAY

01

APPEND NAME[KEY][] <form method="post">   <input type="text" name="fav[c][]">    <input type="text" name="fav[c][]">   <input type="text" name="fav[f][]">   <input type="text" name="fav[f][]">   <input type="submit" value="GO"> </form>

SEND NESTED ARRAY

02

$_POST["FAV"] IS A NESTED ARRAY print_r($_POST["fav"]); print_r($_POST["fav"]["c"]); print_r($_POST["fav"]["f"]);

<form id="form" onsubmit="return ajax()">   <input type="text" name="fav[c][]">    <input type="text" name="fav[c][]">   <input type="text" name="fav[f][]">   <input type="text" name="fav[f][]">   <input type="submit" value="GO"> </form>

AJAX SEND ARRAY (A)

03

function ajax () {   GET FORM DATA   var data = new FormData(document   .getElementById("form"));     AJAX SEND   fetch("SCRIPT.PHP",      { method:"POST", body:data })     .then(res => res.text())     .then(res => console.log(res));     return false; }

AJAX SEND ARRAY (B)

04