This is an example of how to collect dynamic information like workers from a form using PHP. Remember that php is a backend language and runs when the page refreshes, like when you hit the submit button on a form set to method POST, GET or REQUEST. The method is sent to the server and the global variable is created holding the name values of the form.
You retrieve the POST methods using $_POST['name-value'].
IMPORTANT NOTE: Always cleanse your POST values with some kind of relevant cleansing function. I have NOT included any cleansing functions in my example!
See This Link on S.O. for more information on sanitation of inputs.
If the page is currently an html file extension, you must change it to .php
file extension.
Hope this helps!
<?php
$namesOfWorkers = []; // initialize a NEW empty array in PHP
$output = ''; // initialize an empty variable to hold the output into the HTML
// is the $_POST global 'numOfWorkers' key set?
// If not, run through the php conditional to the next elseif section
if(isset($_POST['numOfWorkers'])){
$numOfWorkers = $_POST['numOfWorkers'];
// set an increment and increment each time we loop through
// the amount of workers set in numOfWorkers
$output .= "<form method='post'>"; // create the form html
for($i = 1; $i < $numOfWorkers; $i++){ // runt he incremented for loop against the number set by user
// now use that number to dynamically create input fields
// that would gather the names of the workers...
$output .= 'Name '.$i.' : <input class="namesOfWorkers" name="nameOfWorkers'.$i.'"><br>';
}
$output .= '<input type="submit" name="nameSubmit" id="nameSubmit">
</form>';
// $output now holds the entire new form that gathers the amount of names needed
// to match the number initially entered by user...
// now we see if the names form has been submitted, if so, we push the values into the array
}elseif(isset($_POST['nameSubmit'])){
$j = 0; // another increment for the keys into the array
foreach($_POST as $key => $value){
// lets NOT include the submit buttons key in our array...
if($key !== "nameSubmit"){
// push the number into the key and the $_POST arrays value into the array value
$namesOfWorkers[$j] = $value;
$j++; // increment $j by one with each loop
}
}
}else{
// the code that starts off our form
$output = '<p>Hey there! how many working guys would you like to arrange?</p>
<form method="post">
<input name="numOfWorkers" type="text">
<input type="submit" id="input">
</form>';
}
// $namesOfWorkers now holds the array of your workers key/value(names) pairs
?>
Further down on the SAME PHP page, include the HTML
<!DOCTYPE html>
<html lang="en">
<body>
<?=$output?>
<!--// <?= is a short tag for echo to echo out the $output variable which
Dynamically changes each time we run through the if conditional
within PHP, <? echo $output; ?> could also be used //-->
</body>
</html>
NOTE: To further assist in how this all works, you may want to run a print_r($_POST)
or var_dump($_POST)
at each interval of the conditional if/else
statement to see the POST global arrays key/value pairs and study its output against that block of code...