I need to receive by $_POST a large number of variable numbered variables.
Send from a form:
input id="var_A_1"
input id="var_B_1"
input id="var_C_1"
input id="var_A_2"
input id="var_B_2"
input id="var_C_2"
(The letter type is fixed and reaches N and the number is variable, sometimes up to 10, 20, or 30)
And to receive them I have them like this but too many lines of code are made:
$var_A_1=htmlspecialchars(trim($_POST['var_A_1']));
$var_B_1=htmlspecialchars(trim($_POST['var_B_1']));
$var_C_1=htmlspecialchars(trim($_POST['var_C_1']));
$var_A_2=htmlspecialchars(trim($_POST['var_A_2']));
$var_B_2=htmlspecialchars(trim($_POST['var_B_2']));
$var_C_2=htmlspecialchars(trim($_POST['var_C_2']));
So how could I receive them with a for() ? roughly like the following:
for($n=1; $n<$_POST['cantidad']; $n++){
$var_A_.$n=htmlspecialchars(trim($_POST['var_A_.$n']));
$var_B_.$n=htmlspecialchars(trim($_POST['var_B_.$n']));
$var_C_.$n=htmlspecialchars(trim($_POST['var_C_.$n']));
}
Result: Have the variable with the names:
echo $var_A_1
echo $var_B_1
echo $var_A_2
echo $var_B_2
echo $var_A_3
echo $var_B_3
In total with the letters from A to NY the numbers from 1 to 20 or 30, there are about 300 or 400 variables.
In the variable variables page of the PHP manual they give you some solutions. Try this:
If there are more letters and too many lines of code are recreated, try this:
And perhaps, to avoid unforeseen index not found failures in POSTs, and handle a variable and/or indeterminate number of letters and numbers, you should do it like this:
In this way, even if you have defined more letters or numbers than those actually received by $_POST, only those that exist will be generated.
Simple example based on your comment:
bring back:
You will tell me if it worked for you.