I am trying to use PHP to populate a HTML form and to retain the $_POST
values to assist in editing.
Below is the script showing only two HTML input blocks - there is going to be many more!
I have unsuccessfully tried calling a common function to populate the form and would be grateful if someone could simplify the process to cater for the impending additions:
<?php
declare(strict_types=1);
# echo error_reporting();
# echo ini_get('display_errors');
# die;
$title = 'Test Data';
if(0) : $_POST = []; endif;
$_POST['xxx'][0][0] = $_POST['xxx'][0][0] ?? 'xxx00 NOT SET';
$_POST['xxx'][0][1] = $_POST['xxx'][0][1] ?? 'xxx01 NOT SET';
$_POST['xxx'][0][2] = $_POST['xxx'][0][2] ?? 'xxx02 NOT SET';
$_POST['xxx'][1][0] = $_POST['xxx'][1][0] ?? 'xxx10 NOT SET';
$_POST['xxx'][1][1] = $_POST['xxx'][1][1] ?? 'xxx11 NOT SET';
$_POST['xxx'][1][2] = $_POST['xxx'][1][2] ?? 'xxx12 NOT SET';
?><!DOCTYPE HTML><html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title> <?= $title ?> </title>
<!--
<link rel="stylesheet" href="style-001.css" media="screen">
-->
<style>
h2 {margin: 0.42em 0 0; padding: 0;}
h3 {display: none; text-align: center; margin: 2em auto;}
dt {font-weight: 700;}
label{display: inline-block; width: 11%;}
input{width:80%; background-color: #dfd;}
.bd1 {border: solid 1px #ccc;}
.bgs {background-color: snow; color: #333;}
.mga {margin: 0 auto;}
.p42 {padding: 0.42em;}
.tac {text-align: center;} .tar {text-align: right;}
.w88 {width: 88%; max-width: 888px;}
</style>
</head>
<body>
<pre>
<?php
echo 'TEST: $_POST ==> ';
print_r($_POST);
?>
</pre>
<div class="w88 mga bd1 bgs">
<h1> <?= $title ?> </h1>
<form action="?" method="post">
<dl class="dib bd1">
<dt> Test Data 0 </dt>
<dd>
<label> Jack </label>
<input type="text" name="<?= $_POST['xxx'][0][0] ?>" value="<?= $_POST['xxx'][0][0] ?>">
<br>
<label> Fred </label>
<input type="text" name="<?= $_POST['xxx'][0][1] ?>" value="<?= $_POST['xxx'][0][1] ?>">
<br>
<label> Harry </label>
<input type="text" name="<?= $_POST['xxx'][0][2] ?>" value="<?= $_POST['xxx'][0][1] ?>">
<br><br>
</dd>
</dl>
<dl class="dib bd1">
<dt> Test Data 1 </dt>
<dd>
<label> Jack </label>
<input type="text" name="<?= $_POST['xxx'][1][0] ?>" value="<?= $_POST['xxx'][1][0] ?>">
<br>
<label> Fred </label>
<input type="text" name="<?= $_POST['xxx'][1][1] ?>" value="<?= $_POST['xxx'][1][1] ?>">
<br>
<label> Harry </label>
<input type="text" name="<?= $_POST['xxx'][1][2] ?>" value="<?= $_POST['xxx'][1][2] ?>">
<br><br>
</dd>
</dl>
<input type="submit" value="submit">
</form>
</div>
</body>
</html>